【发布时间】:2017-05-25 14:29:31
【问题描述】:
我正在尝试将原始数据分组:
items:
[
{
category: "blog",
id : "586ba9f3a36b129f1336ed38",
content : "foo, bar!"
},
{
category: "blog",
id : "586ba9f3a36b129f1336ed3c",
content : "hello, world!"
},
{
category: "music",
id : "586ba9a6dfjb129f1332ldab",
content : "wow, shamwow!"
},
]
到
[
{
category: "blog",
items:
[
{
id : "586ba9f3a36b129f1336ed38",
content : "foo, bar!"
},
{
id : "586ba9f3a36b129f1336ed3c",
content : "hello, world!"
},
]
},
{
category: "music",
items:
[
{
id : "586ba9a6dfjb129f1332ldab",
content : "wow, shamwow!"
}
]
}
]
这样的格式可以帮助我在前端打印相同的类别数据。
category 字段的内容是动态的,所以我不确定如何将其存储到临时对象并对其进行排序,有什么想法吗?
(我想不出更好的问题标题,如果你有更好的标题,请编辑。)
【问题讨论】:
-
如果有像 Lodash 这样的库,那就是
_.groupBy(items, 'category')
标签: javascript arrays object multidimensional-array categorization