1
我正在嘗試使用下劃線過濾出對象的某些屬性。以下代碼的開頭按預期工作,但.pick
不起作用。我旨在將返回對象的屬性限制爲.pick
方法中列出的那些字符串。如何正確使用lodash挑選方法
var result = _.chain(data)
.each(function(item) {
item.answers = [];
_.each(data, function(object) {
if (item.id === object.id) {
item.answers.push({
id: object.answer_id,
email: object.answer_email,
date: object.answer_date
});
}
});
item = _.pick(item,
'id',
'owner_id',
'url',
'enabled',
'review_date',
'answers'
);
})
.uniq(function(item) {
return item.id;
})
.value();
我開始, '數據' 數組,看起來像這樣:
[
{
id: '8ffdf27b-5a90-478a-b263-dhhdhdhhdhd',
answer_date: Fri Oct 30 2015 14:35:07 GMT-0400 (EDT),
answer_id: 1,
answer_email: '[email protected]',
owner_id: 5,
url: 'media/5-4a3640ac-ec13-fhhfh-ac0a-fhjhdhhdhd.jpg',
enabled: false,
review_date: Sun Nov 01 2015 13:57:32 GMT-0500 (EST)
}, ...
]
返回數組 '應該' 看起來像這樣:
[
{
id: '8ffdf27b-5a90-478a-b263-dhhdhdhhdhd',
owner_id: 5,
url: 'media/5-4a3640ac-ec13-fhhfh-ac0a-fhjhdhhdhd.jpg',
enabled: false,
review_date: Sun Nov 01 2015 13:57:32 GMT-0500 (EST),
answers: [{...}, {...}]
}, ...
]
而是目前的樣子這個:
[
{
id: '8ffdf27b-5a90-478a-b263-dhhdhdhhdhd',
answer_date: Fri Oct 30 2015 14:35:07 GMT-0400 (EDT),
answer_id: 1,
answer_email: '[email protected]',
owner_id: 5,
url: 'media/5-4a3640ac-ec13-fhhfh-ac0a-fhjhdhhdhd.jpg',
enabled: false,
review_date: Sun Nov 01 2015 13:57:32 GMT-0500 (EST),
answers: [{...}, {...}]
}, ...
]