我有以下lodash
函数,用于将数据分组并返回:
[
{
"tranid": "100",
"trandate": "2019-08-20T07:00:00.000Z",
"account": "1",
"currency": "1",
"departement": "1",
"location": "1",
"list": [ "1", "2" ]
},
{
"tranid": "200",
"trandate": "2019-08-20T07:00:00.000Z",
"account": "1",
"currency": "1",
"departement": "1",
"location": "1",
"list": [ "3", "1260" ]
}
]
但是我真的很想让它返回:
[
{
"tranid": "100",
"trandate": "8/20/2019",
"account": "1",
"currency": "1",
"departement": "1",
"location": "1",
"list": [
{ "docnumber": "1" },
{ "docnumber": "2" }
]
},
{
"tranid": "200",
"trandate": "8/20/2019",
"account": "1",
"currency": "1",
"departement": "1",
"location": "1",
"list": [
{ "docnumber": "3" },
{ "docnumber": "1260" }
]
}
]
具体来说,我坚持的部分是如何制作:
"list":["1","2"]
变成
"list":[{"docnumber":"1"},{"docnumber":"2"}].
我知道组功能中的list: _.map(v, 'docnumber')
需要更改,但还不太清楚如何更改。任何帮助都会很棒,谢谢。
function group(data) {
var group = lo(data).chain(data).groupBy("tranid").map(function(v,i) {
return {
tranid: i,
processed: _.get(_.find(v, 'processed'), 'processed'),
trandate: _.get(_.find(v, 'trandate'), 'trandate'),
account: _.get(_.find(v, 'account'), 'account'),
currency: _.get(_.find(v, 'currency'), 'currency'),
departement: _.get(_.find(v, 'departement'), 'departement'),
location: _.get(_.find(v, 'location'), 'location'),
list: _.map(v, 'docnumber')
}
}).value();
return group;
}
答案 0 :(得分:0)
像这样传递函数:
list: _.map(v, docnumber => ({ docnumber }));
这使用了ES6对象符号的简写形式。在普通JavaScript中,它将是:
list: v.map(docnumber => ({ docnumber }));
在ES5中,它将是:
list: v.map(function(n) {
return {
docnumber: n
};
})