2015-07-21 180 views
1

我正在使用node.js和lodash。使用lodash中的键值合并一个对象数组?

我有这样的数据:

[ 
    { 
    to: [ '[email protected]', '[email protected]' ], 
    submittedSubs: [ [Object] ] 
    }, 
    { 
    to: [ '[email protected]', '[email protected]' ], 
    submittedSubs: [ [Object], [Object], [Object] ] 
    } 
] 

我希望把它变成在那里它是由to

[ 
    { 
    to: '[email protected]', 
    submittedSubs: [ [Object],[Object], [Object], [Object] ] 
    }, 
    { 
    to: '[email protected]', 
    submittedSubs: [ [Object] ] 
    }, 
    { 
    to: '[email protected]', 
    submittedSubs: [ [Object], [Object], [Object] ] 
    } 
] 

我该怎么做“分类”像这样的数据?

我已经试过这样:

spam[0].to.push('[email protected]'); 
spam[0].to.push('[email protected]'); 
spam[1].to.push('[email protected]'); 
spam[1].to.push('[email protected]'); 

console.log('data is',spam); 

var byUser=[]; 
_.each(spam, function(data){ 
    _.each(data.to,function(addr){ 
     byUser.push({to:addr,submittedSubs:data.submittedSubs}); 
    }); 
}); 
console.log('attempt',_.merge(byUser)); 

但是,这给了我这样的:

[ { to: '[email protected]', submittedSubs: [ [Object] ] }, 
{ to: '[email protected]', submittedSubs: [ [Object] ] }, 
{ to: '[email protected]', submittedSubs: [ [Object], [Object], [Object] ] }, 
{ to: '[email protected]', submittedSubs: [ [Object], [Object], [Object] ] } ] 
+0

看起来最简单的方法是以电子邮件地址为关键字创建一个对象,其值为一个对象数组。然后可以将它转换回数组并在'to'字段中排序。 –

回答

1

这会为你工作:

var unique = {}; 
byUser.forEach(function(user) { 
    unique[user.to] = unique[user.to] || []; 
    unique[user.to] = unique[user.to].concat(user.submittedSubs); 
}); 
unique = Object.keys(unique).map(function (key, i) { 
    return {to: key, submittedSubs: unique[key]}; 
}); 

/* 
[ { to: '[email protected]', submittedSubs: [ [Object] ] }, 
{ to: '[email protected]', submittedSubs: [ [Object] ] }, 
{ to: '[email protected]', submittedSubs: [ [Object], [Object], [Object], [Object] ] } ] 
*/ 

我站在因此这应该可以通过电话来实现_.uniq的后退功能,但我无法按照您需要的方式工作。

你应该能够最终阵列上使用_.uniq

_.uniq(byUser, "to"); 

/* 
[ { to: '[email protected]', submittedSubs: [ [Object] ] }, 
{ to: '[email protected]', submittedSubs: [ [Object] ] }, 
{ to: '[email protected]', submittedSubs: [ [Object], [Object], [Object] ] } ] 
*/ 
+0

但是,我想'foo @ bar'包含四个'submittedSubs'。一个来自第一个对象,另外三个来自第二个对象。但是有了它,它只包含一个。 – Houseman

+0

请参阅编辑。这是丑陋的,但它的工作原理。 – brandonscript

1

我想有一些不错的lodash设施,以缩短这个了一点,但这里有一个香草JS的解决方案:

var data = [ 
    { 
    to: [ '[email protected]', '[email protected]' ], 
    submittedSubs: [{ id: 'sub1' }] 
    }, 
    { 
    to: [ '[email protected]', '[email protected]' ], 
     submittedSubs: [{ id: 'sub2' }, { id: 'sub3' }, { id: 'sub4' }] 
    } 
]; 

var emailSubsMap = data.reduce(function(result, record) { 
    record.to.forEach(function(email) { 
     result[email] = (result[email] || []) 
      .concat(record.submittedSubs); 
    }); 
    return result; 
}, {}); 

var formatted = Object.keys(emailSubsMap).map(function(email) { 
    return { to: email, submittedSubs: emailSubsMap[email] }; 
}).sort(function(a, b) { 
    return a.to <= b.to ? -1 : 1; 
}); 

console.log(JSON.stringify(formatted)); 

(格式化)控制台输出:

[ 
    { 
     "to": "[email protected]", 
     "submittedSubs": [ 
      { "id": "sub1" } 
     ] 
    }, 
    { 
     "to": "[email protected]", 
     "submittedSubs": [ 
      { "id": "sub2" }, 
      { "id": "sub3" }, 
      { "id": "sub4" } 
     ] 
    }, 
    { 
     "to": "[email protected]", 
     "submittedSubs": [ 
      { "id": "sub1" }, 
      { "id": "sub2" }, 
      { "id": "sub3" }, 
      { "id": "sub4" } 
     ] 
    } 
] 

请注意,我只是为了测试目的而模拟了submittedSubs对象的外观。

JSFiddle Example

约分拣一对夫妇的注意事项:

+0

当我运行这个时,我得到'[TypeError:Object [email protected]没有方法'forEach']' – Houseman

+0

你确定你把它正确地复制了吗?请参阅jsfiddle示例以查看它是否正常工作:http:// jsfiddle。net/vgexn06L/1/ – jmar777

+0

另外,是否有可能不是所有数据的形状都像这个例子那样?例如,也许一些'to'值是单值而不是数组? – jmar777

相关问题