data =[
{
"origin": "Mumbai",
"destination": "Chennai"
},
{
"origin": "Mumbai",
"destination": "Delhi",
},
{
"origin": "Pune",
"destination": "Brahmapur",
},]
期望的输出:
[
{"Mumbai" : ["Chennai","Delhi"]},
{"Pune" : ["Brahmapur"]}
]
以下是我要做的事情:
var list = _.pluck(data, 'destination'); // it gives the array of destinations
var gList = _.groupBy(data, 'origin'); // it gives me json which are grouped by the origin key.
我尝试在下面结合使用 pluck 和 groupby ,它会给我一些特定原始密钥的目标数组,但显然它不起作用。
var gpList = _.groupBy(_.pluck(data, 'parname'), 'category');
答案 0 :(得分:1)
你几乎可以选择那些功能。
首先要做的是groupBy来源。这将返回一个如下所示的对象:
{
Mumbai: [ { origin: 'Mumbai', destination: 'Chennai' },
{ origin: 'Mumbai', destination: 'Delhi' } ],
Pune: [ { origin: 'Pune', destination: 'Brahmapur' } ]
}
var result = _.chain(data)
.groupBy('origin')
.mapObject(group => _.pluck(group, 'destination'))
.value()