I am currently working with a JSON object that looks like this:
var testJSON = [
{ "AssetA": "asset_a", "AssetB": "asset_b" },
{ "AssetA": "asset_a", "AssetB": "asset_b" },
{ "AssetA": "asset_c", "AssetB": "asset_d" },
{ "AssetA": "asset_c", "AssetB": "asset_e" }];
My objective is to iterate through the object and add duplicate keys to a new array called usedAssets. I have written some code to achieve this:
var usedAssets = [];
for (var key in testJSON) {
console.log("Current key: " + key + " " + "value: : " + testJSON[key].AssetA);
console.log("Current key: " + key + " " + "value: : " + testJSON[key].AssetB);
// check if in array
if ((isInArray(testJSON[key].AssetA, usedAssets) || isInArray(testJSON[key].AssetB, usedAssets))) {
break;
}
else {
usedAssets.push(testJSON[key].AssetA);
usedAssets.push(testJSON[key].AssetB);
}
}
console.log(usedAssets);
function isInArray(value, array) {
return array.indexOf(value) > -1;
}
However, the output I get is an array with only asset_a and asset_b. The usedAssets array should actually contain asset_a, asset_b, and asset_c. Ultimately, my goal is to determine at the end of the loop how many times each unique asset was used.