Take this array:
[
{"color": "blue","type": 1},
{"color": "red","type": 1},
{"color": "blue","type": 1},
{"color": "green","type": 1},
{"color": "green","type": 1},
{"color": "red","type": 2},
{"color": "red","type": 1},
{"color": "green","type": 2},
{"color": "red","type": 3},
];
How would I go about finding which "color" has a different "type" (than all other objects with the same "name") in the array?
I want to be able to loop through this array and create a second array that would look like this:
{red, green}
Notice blue is ommited because all of the objects with "color":"blue" have the same "type"
The closest I have gotten is this: https://jsfiddle.net/0wgjs5zh/ but it adds all colors into the array appended with the different types:
arr.forEach(function(item){
if(newArr.hasOwnProperty(item.color+ '-' +item.type)) {
// newArr[item.color+ '-' +item.type].push(item);
}
else {
newArr[item.color+ '-' +item.type] = item;
}
});
// RESULT
{blue-1, green-1, green-2, red-1, red-2, red-3}
colornotname, right?{ blue: 1, blue: 2, blue: 2}and{ blue: 2, blue: 1, blue: 1 }be the same? Or do you know1is the base type?