JavaScript - 根据属性对对象进行分组

3
我的问题有些类似于 javascript | 对象分组

我的输入对象是:

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

我不想将拥有相同属性的对象分组成一个数组。我找不到他们将其推入数组的位置。请帮我修复它 :(


输入和输出有什么区别,尽管顺序不同? - Nina Scholz
1
这不是分组,而是排序。 - Yeldar Kurmangaliyev
你正在尝试对数组进行排序吗?按照哪个属性来排序? - Weedoze
不是。我正在尝试对对象进行分组。就像所有属于技术细节组的对象应该相邻放置,这样当我在表格中显示时,所有项目都将在下一行中。 - Karthi
2个回答

4
你可以将项目分组,然后将所有组合并到一个数组中。

It works by generating an object with the group as property an the items as items in the array.

In the next step, the object's values are taken and a new array is generated by concatination of the items 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; }

或者按排序。

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; }


完美的!你能详细说明一下你实际执行的步骤吗?对我来说,这一切都像希腊语和拉丁语一样难懂。 - Karthi

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接