Javascript - grouping objects by properties

my question is somewhat similar to javascript | Grouping objects .

my input obj is

[
  {
    "name":"Display",
    "group":"Technical detals",
    "id":"60",
    "value":"4"
  },
  {
    "name":"Manufacturer",
    "group":"Manufacturer",
    "id":"58",
    "value":"Apple"
  },
  {
    "name":"OS",
    "group":"Technical detals",
    "id":"37",
    "value":"Apple iOS"
  }
]

      

and my required result is

 [
  {
    "name":"Display",
    "group":"Technical detals",
    "id":"60",
    "value":"4"
  },
  {
    "name":"OS",
    "group":"Technical detals",
    "id":"37",
    "value":"Apple iOS"
  },
  {
    "name":"Manufacturer",
    "group":"Manufacturer",
    "id":"58",
    "value":"Apple"
  }

]

      

when i tried to follow the answer i got

[[[object Object] {
  group: "Technical detals",
  id: "60",
  name: "Display",
  value: "4"
}, [object Object] {
  group: "Technical detals",
  id: "37",
  name: "OS",
  value: "Apple iOS"
}], [[object Object] {
  group: "Manufacturer",
  id: "58",
  name: "Manufacturer",
  value: "Apple"
}]]

      

I don't want to group objects with the same property into one array. I couldn't find out where they are pushing it into the array. help me fix this :(

+3


source to share


2 answers


You can group elements and concat of all groups in one array.

It works by creating an object with a class as a property and elements as elements in an array.

In the next step, the values โ€‹โ€‹of the object are taken, and a new array is generated by concatenating the elements with Array#reduce

.

{                                                                              // hash
    "Technical detals": [
        {
            name: "Display",
            group: "Technical detals",
            id: "60",
            value: "4"
        },
        {
            name: "OS",
            group: "Technical detals",
            id: "37",
            value: "Apple iOS"
        }
    ],
    Manufacturer: [
        {
            name: "Manufacturer",
            group: "Manufacturer",
            id: "58",
            value: "Apple"
        }
    ]
}

      

var data = [{ name: "Display", group: "Technical detals", id: "60", value: "4" }, { name: "Manufacturer", group: "Manufacturer", id: "58", value: "Apple" }, { name: "OS", group: "Technical detals", id: "37", value: "Apple iOS" }],
    groups = Object.create(null),
    result = [];

data.forEach(function (a) {
    groups[a.group] = groups[a.group] || [];
    groups[a.group].push(a);    
});

result = Object.keys(groups).reduce(function (r, k) {
    return r.concat(groups[k]);
}, []);

console.log(result);
      

.as-console-wrapper { max-height: 100% !important; top: 0; }
      

Run codeHide result




Or just sort by group

.

var data = [{ name: "Display", group: "Technical detals", id: "60", value: "4" }, { name: "Manufacturer", group: "Manufacturer", id: "58", value: "Apple" }, { name: "OS", group: "Technical detals", id: "37", value: "Apple iOS" }];

data.sort(function (a, b) {
    return a.group.localeCompare(b.group);
});

console.log(data);
      

.as-console-wrapper { max-height: 100% !important; top: 0; }
      

Run codeHide result


+4


source


Without sorting, you can group by the provided property ( by

) like this:



function quasiSort(a,by){
  var h = a.reduce((h,e) => h[e[by]] ? (h[e[by]].push(e), h)
                                     : (h[e[by]] = [e], h), {});
  return Object.keys(h).reduce((r,k) => r.concat(h[k]),[]);
}
var data = [{ "name":"Display",
             "group":"Technical detals",
                "id":"60",
             "value":"4"
            },
            { "name":"Manufacturer",
             "group":"Manufacturer",
                "id":"58",
             "value":"Apple"
            },
            { "name":"OS",
             "group":"Technical detals",
                "id":"37",
             "value":"Apple iOS"
            }
           ];
           
console.log(quasiSort(data,"group"));
      

Run codeHide result


0


source







All Articles