Extension of this question: using lodash .groupBy. how to add your own keys for grouped output?
Let's change the array to this one:
[
{
"name": "jim",
"color": "blue",
"order": 1,
"age": "22"
},
{
"name": "Sam",
"color": "blue",
"order": 1,
"age": "33"
},
{
"name": "eddie",
"color": "green",
"order": 3
"age": "77"
}
]
The order field has been added. In the previous question, this
var result = _.chain(data)
.groupBy("color")
.pairs()
.map(function(currentItem) {
return _.object(_.zip(["color", "users"], currentItem));
})
.value();
console.log(result);
was used to change the array into
[
{
color: "blue",
users: [
{
"name": "jim",
"color": "blue",
"age": "22"
},
{
"name": "Sam",
"color": "blue",
"age": "33"
}
]
},
{
color: "green",
users: [
{
"name": "eddie",
"color": "green",
"age": "77"
}
]
}
]
Now, I want to add the "order" field to the group by such that the array looks like
[
{
color: "blue",
order: 1,
users: [
{
"name": "jim",
"color": "blue",
"age": "22"
},
{
"name": "Sam",
"color": "blue",
"age": "33"
}
]
},
{
color: "green",
order: 3,
users: [
{
"name": "eddie",
"color": "green",
"age": "77"
}
]
}
]