简体   繁体   English

我想将两个Javascript数组与Lodash结合使用(而不是concat)

[英]I want to combine two Javascript Array with Lodash (not concat)

I have two array like below 我有如下两个数组

  const registers = [{ user: 'Allen', type: 'phone' }, { user: 'Eric', type: 'email' }];
  const dates = ['20171225', '20180914'];

and I want they comebine like this 我希望他们像这样

const result = [
  { user: 'Allen', type: 'phone', date: '20171225' }
  { user: 'Allen', type: 'phone', date: '20180914' }
  { user: 'Eric', type: 'email', date: '20171225' }
  { user: 'Eric', type: 'email', date: '20180914' }
];

I think there is an proper function of Lodash to use in this case, but I can't find it by myself. 我认为在这种情况下可以使用Lodash的适当功能,但我自己找不到。 Is there any cool geek can help to get out of this hell. 有没有很酷的怪胎可以帮助摆脱这个地狱。 Thanks~ 谢谢〜

I agree to use of plugins instead of rewrite what people have made available. 我同意使用插件,而不是重写人们提供的内容。 But when It's simple, using a plugin is complicating things, it's better to use the simplier soluce when there is one. 但是,简单来说,使用插件会使事情变得复杂,最好在有插件的情况下使用简单的解决方案。

 const registers = [{ user: 'Allen', type: 'phone', }, { user: 'Eric', type: 'email', }]; const dates = [ '20171225', '20180914', ]; const arr = registers.reduce((tmp, x) => [ ...tmp, ...dates.map(y => ({ ...x, date: y, })), ], []); console.log(arr); 

One method is with reduce + forEach, but any double loop should work. 一种方法是用reduce + forEach,但是任何双循环都应该起作用。

 const registers = [{ user: 'Allen', type: 'phone' }, { user: 'Eric', type: 'email' }]; const dates = ['20171225', '20180914']; let answer = registers.reduce((acc, n) => { dates.forEach(x => { acc.push({user: n.user, type: n.type, date: x}); }); return acc; }, []) console.log(answer); 

In this case, use of lodash doesn't yield much code reduction (you could simply replace the calls to _.map(a, ...) with a.map(...) ). 在这种情况下,使用lodash不会减少太多代码(您可以简单地_.map(a, ...)的调用替换为a.map(...) )。 But well, here goes: 但是好吧,这里是:

 const registers = [{ user: 'Allen', type: 'phone' }, { user: 'Eric', type: 'email' }]; const dates = ['20171225', '20180914']; const result = _.map(registers, ({ user, type }) => _.map(dates, (date) => ({ user, type, date }))); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script> 

As OP stated to use lodash , Here is a solution 正如OP所述使用lodash,这是一个解决方案

Lodash Variant Lodash变体

 var registers = [{ user: 'Allen', type: 'phone' }, { user: 'Eric', type: 'email' }]; var dates = ['20171225', '20180914']; let result = _.map(registers, function(value, index, _source){ return _.extend(value, { date: (dates[index] || null)}); }); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.min.js"></script> 

Vanilla JS Variant 香草JS变体

 var registers = [{ user: 'Allen', type: 'phone' }, { user: 'Eric', type: 'email' }]; var dates = ['20171225', '20180914']; let result = registers.map(function(value, index, _source){ return Object.assign(value, { date: (dates[index] || null)}); }); console.log(result); 

Updated Answer 更新的答案

 var registers = [{ user: 'Allen', type: 'phone' }, { user: 'Eric', type: 'email' }]; var dates = ['20171225', '20180914']; const result = _.flatten(_.map(registers, function(value, index, _source){ return _.map(dates, (v, i, _s) => _.extend(value, { date: v})); })); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM