我有两个如下所示的数组
const registers = [{ user: 'Allen', type: 'phone' }, { user: 'Eric', type: 'email' }];
const dates = ['20171225', '20180914'];
我希望他们像这样结合
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' }
];
我认为在这种情况下可以使用 Lodash
的正确函数,
但我自己找不到它。
有没有酷极客可以帮助摆脱这个 hell 。谢谢~
最佳答案
我同意使用插件而不是重写人们提供的内容。但是,当事情很简单时,使用插件会使事情变得复杂,所以最好在有解决方案时使用更简单的解决方案。
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);
关于javascript - 我想用 Lodash 组合两个 Javascript 数组(不是 concat),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52326819/