javascript - 组合两个元素数量不同的数组

标签 javascript arrays concatenation

假设我们有这两个数组:

x = [1, 2, 3];
y = ['a', 'b'];

将它们结合起来并获得以下结果的最佳方法是什么:

newArray = ['1a', '1b', '2a', '2b', '3a', '3b'];

最佳答案

这是一种方法:

x.reduce(function(arr, x) {
  return arr.concat(y.map(function(y) {
    return x + y;
  }));
}, []);
//=> ["1a", "1b", "2a", "2b", "3a", "3b"]

关于javascript - 组合两个元素数量不同的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33980951/

相关文章:

javascript - 在node.js中与数据库通信

javascript - Node.js 服务器 "404 not found"消息到 404.html 页面

javascript - 带有无限参数的构造函数是如何创建的?

php - 使用 php mysql 显示多维数组的 JSON 代码

jQuery 将元素 ID 传递到 jquery 语句中?

python - 在 Pandas 中动态附加数据框

javascript - 是否有任何支持 Internet Explorer 8 的热图 javascript 库?

javascript - 通过比较另一个数组中的值在 JavaScript 中创建数组

c++ - 从 Arduino 中的字符数组中选择项目

python - 将 Pandas DataFrames 与多索引列和不规则时间戳连接起来