Array Filter and create a new object
Filter by group, type, color
by Udhay
JavaScript
myArray = [
{group: "one", type: "normal", color: "blue"},
{group: "one", type: "normal", color: "blue"},
{group: "one", type: "normal", color: "green"},
{group: "one", type: "normal", color: "black"}
];
var res = myArray.reduce(function(res, currentValue) {
if ( res.indexOf(currentValue.group) === -1 ) {
res.push(currentValue.group);
}
return res;
}, []).map(function(group) {
return {
group: group,
type: myArray.reduce(function(getType, currentValue) {
if ( getType.indexOf(currentValue.type) === -1 ) {
getType.push(currentValue.type);
}
return getType;
},[]),
color: myArray.filter(function(_el) {
return _el.group === group;
}).map(function(_el) { return _el.color; })
}
});
console.log(res);