javascript - 我想用 Lodash 组合两个 Javascript 数组(不是 concat)

标签 javascript arrays lodash

我有两个如下所示的数组

  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/

相关文章:

javascript - 在 Javascript 中调用它之前确保嵌套对象值存在的最佳方法

javascript - 根据其位置更改导航栏颜色。颜色不变

javascript - 在数组的 2/3 上调用自身的排序算法

javascript - lodash 四舍五入到小数点后一位而不是 2 位

javascript - 上一个文件进度达到 100% 与停止/完成事件之间存在巨大差距?

javascript - IE过滤器将图像设置为背景

java - BFS 的三个计数器阵列和两个具有不同启发式的 A* 的相同结果

Java 给定整数的字符串排列

javascript - Lodash _.setWith 自定义路径

javascript - 在此示例中如何将数组值与对象属性进行比较?