javascript - 按属性对对象进行分组

时间:2017-03-31 08:32:33

标签: javascript arrays object

我的问题与javascript | Object grouping有些相似。

我的输入obj是

[
  {
    "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"
  }
]

我的所需输出是

 [
  {
    "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"
  }

]

当我试图实现答案时,我得到了

[[[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"
}]]

我不想将具有相同属性的对象分组到一个数组中。我无法找到他们将其推入阵列的位置。帮我修复它:(

2 个答案:

答案 0 :(得分:4)

您可以将项目和concat所有组分组到一个数组中。

  

它的工作原理是生成一个对象,该对象具有作为属性的组,作为数组中项目的项目。

     

在下一步中,将获取对象的值,并通过将项目与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; }

或者只按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; }

答案 1 :(得分:0)

如果没有排序,您可以按提供的属性(by)进行分组,如下所示;

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"));

相关问题