javascript - Create arrays of duplicates js -
for example: have array looks this:
let arr = [{ name: "peter" }, { name: "peter" }, { name: "john" }, { name: "peter" }, { name: "sarah" }, { name: "john" }]
i need create array, this:
let duparray = [ [{ name: "peter" }, { name: "peter" }, { name: "peter" }], [{ name: "john" }, { name: "john" }], [{ name: "sarah" }] ]
again, it's not duplicating, it's creating new array duplicates grouped.
reduce them map , values():
let arr = [{ name: "peter" }, { name: "peter" }, { name: "john" }, { name: "peter" }, { name: "sarah" }, { name: "john" }]; let duparr = [...arr.reduce((a,b) => a.set(b.name , (a.get(b.name) || []).concat(b)), new map()).values()]; console.log(duparr);
Comments
Post a Comment