Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let array = [
- {
- id: "1",
- name: "Table1",
- color: "red"
- },
- {
- id: "2",
- name: "Table1",
- color: "magenta"
- },
- {
- id: "3",
- name: "Table1",
- color: "red"
- },
- {
- id: "4",
- name: "Table2",
- color: "red"
- }
- ];
- let groupBy = (array, key) => {
- let groups = [];
- let set = new Set();
- array.forEach(e => {
- set.add(e[key]);
- });
- set.forEach(s => {
- const [...el] = array.filter(f => f[key] === s);
- groups.push(el);
- });
- return groups;
- };
- let groupedArray = groupBy(array, "name");
- let newArray = [];
- groupedArray.forEach( e => {
- newArray.push(groupBy(e, "color"));
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement