javascript | Object grouping

后端 未结 10 1272
不思量自难忘°
不思量自难忘° 2020-11-27 05:29

I have an object. It looks like below:

[
  {
    \"name\":\"Display\",
    \"group\":\"Technical detals\",
    \"id\":\"60\",
    \"value\":\"4\"
  },
  {
           


        
10条回答
  •  渐次进展
    2020-11-27 06:21

    Try

    let g = (d,h={},r={},i=0)=>(d.map(x=>(y=x.group,h[y]?1:(h[y]=++i,r[h[y]-1]=[]),r[h[y]-1].push(x))),r);
    console.log( g(data) );
    

    let 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"
      }
    ];
    
    
    let g = (d,h={},r={},i=0)=>(d.map(x=>(y=x.group,h[y]?1:(h[y]=++i,r[h[y]-1]=[]),r[h[y]-1].push(x))),r);
    
    console.log(g(data));

提交回复
热议问题