JSFiddle - React, Tailwind, and code Playground
by Alexey
JavaScript
var data = [{
label: "Book1",
data: "US edition",
id: 465,
name: 'edit'
},
{
label: "Book1",
data: "UK edition",
id: 455,
name: 'read'
},
{
label: "Book2",
data: "CAN edition",
id: 435,
name: 'edit'
}
];
var seen = {};
data = data.filter(function(entry) {
var previous;
// Have we seen this label before?
if (seen.hasOwnProperty(entry.label)) {
// Yes, grab it and add this data to it
previous = seen[entry.label];
previous.data.push(entry.data);
// Don't keep this entry, we've merged it into the previous one
return false;
}
// entry.data probably isn't an array; make it one for consistency
if (!Array.isArray(entry.data)) {
let arr = [];
let obj = {};
obj.permisstions = [];
arr.push(obj);
entry.data = arr;
}
// Remember that we've seen it
seen[entry.label] = entry;
// Keep this one, we'll merge any others that match into it
return true;
});
console.log(seen)