最佳答案
我从一个 API 返回了这个样本数据。
我正在使用 Lodash 的 _.groupBy将数据转换成一个我可以使用得更好的对象。
返回的原始数据如下:
[
{
"name": "jim",
"color": "blue",
"age": "22"
},
{
"name": "Sam",
"color": "blue",
"age": "33"
},
{
"name": "eddie",
"color": "green",
"age": "77"
}
]
我希望 _.groupBy函数返回一个如下的对象:
[
{
color: "blue",
users: [
{
"name": "jim",
"color": "blue",
"age": "22"
},
{
"name": "Sam",
"color": "blue",
"age": "33"
}
]
},
{
color: "green",
users: [
{
"name": "eddie",
"color": "green",
"age": "77"
}
]
}
]
目前我在吸毒
_.groupBy(a, function(b) { return b.color})
就是把这个还回去。
{blue: [{..}], green: [{...}]}
分组是正确的,但我真的想添加键我想(color,users)。这可能使用 _.groupBy吗?或其他 LoDash实用程序?