Struggling to find a way to limit the output of my comparison between attribute values in an object, I am only looking for one output per ID.
Any suggestions?
//example JSON
var obj1 = {
"Summary" :
[
{
"ID" : "1234",
"Name" : "John",
"Status" : "Green",
},
{
"ID" : "5678",
"Name" : "Mike",
"Status" : "Green",
},
{
"ID" : "9012",
"Name" : "Tom",
"Status" : "Red",
}
]
};
//my code
var attributesvalues = ["Mike", "Green", "Red"];
var sg1 = [];
var k;
var l;
//push name of each attribute value to a new array
//one example
sg1.push(attributesvalues[0]);
//go through each ID, if name value exist, push "1", else push ""
for (k = 0; k < obj1.Summary.length; k++) {
for (l in obj1.Summary[k]) {
if (sg1[0] == obj1.Summary[k][l]){
sg1.push("1");
}
else{
sg1.push("");
}
}
}
The desired output should be - 4 values, name + a "1" or "" for each ID(3)
sg1 = ["Mike", "", "1", ""]
sg2 = ["Green", "1", "1", ""]
Instead, the current output is - the name + a "1" or "" for each attribute.
sg1 = ["Mike", "", "", "", "", "1", "", "", "", ""]
sg2 = ["Green", "", "", "1", "", "", "1", "", "", ""]
Any additional guidance or tips you can offer would be highly appreciated. Still trying to get the hang of JS.