【问题标题】:Using es6 spread to concat multiple arrays使用 es6 spread 连接多个数组
【发布时间】:2017-04-17 17:01:55
【问题描述】:

我们都知道你可以做到:

let arr1 = [1,2,3];
let arr2 = [3,4,5];
let arr3 = [...arr1, ...arr2]; // [1,2,3,3,4,5]

但是如何让这个动态连接 N 个数组呢?

【问题讨论】:

  • 您可以将动态数组存储在数组中...
  • 或以数组为值的对象。无法迭代未知数量的单个变量
  • 为什么必须使用扩展运算符? concat 方法仍然有效...
  • Array.prototype.concat 不适合你吗? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 嗯,哦,所以 ES5ify Function.prototype.call.bind(Array.prototype.concat) :)

标签: javascript ecmascript-6


【解决方案1】:

一种选择是使用reduce

let arrs = [[1, 2], [3, 4], [5, 6]];
arrs.reduce((a, b) => [...a, ...b], []);

当然,这是一个缓慢的解决方案(二次时间)。或者,如果您可以使用 Lodash,_.flatten 会完全按照您的意愿行事,而且效率更高(线性时间)。

编辑

或者,改编自下面 Xotic750 的评论,

[].concat(...arrs);

这应该是有效的(线性时间)。

【讨论】:

  • 一个你有你的数组在一个数组中,那么你可以。 Array.prototype.concat.apply([], arr) 虽然这不再使用扩展运算符了。
  • @Xotic750 不错,但是您可以使用扩展运算符来做到这一点!查看我的编辑。
  • 请注意,在打字稿中,如果您希望返回值为number[] 而不是any[],则需要强制输入空数组(例如([] as number[]).concat(...arrs)
【解决方案2】:

另一种选择可能是:

const nArrays = [
  [1, 2, 3, 4, 5],
  [6, 7, 8, 9],
  [10, 11]
];
const flattened = [].concat(...nArrays);
console.log(flattened)

【讨论】:

  • 从 ES2019 开始,你可以使用 nArrays.flat()
【解决方案3】:

let fruits = ["apples", "bananas", "pears"];
let vegetables = ["corn", "potatoes", "carrots"];

let produce = [...fruits, ...vegetables];


console.log(produce);

【讨论】:

  • 这个给定的代码与给定的问题完全相同。这不是为 N 个数组制作动态连接解决方​​案的答案。
【解决方案4】:

以下解决方案适用于我(ES6 中的扩展运算符):

let array = ['my','solution','works'];
let newArray = [];
let newArray2 = [];
newArray.push(...array); //adding to same array
newArray2.push([...array]); //adding as child/leaf/sub-array
console.log(newArray);
console.log(newArray2);

【讨论】:

    【解决方案5】:

    您不能仅使用扩展语法来做到这一点,因为扩展语法要求您提前知道要连接多少个数组。但是,您可以编写以下函数:

    function concatN(...arguments) {
        let accumulator = [];
        for(let arg = 0; arg < arguments.length; arg = arg + 1) {
            accumulator = [...accumulator, ...arguments[arg]];
        }
        return accumulator;
    }
    

    不过,它可能不会很有效(重复使用扩展语法是 O(n²))。使用Array.prototype.concat会更好。你可以这样做:

    [].concat(all, of, your, arrays);
    

    【讨论】:

      【解决方案6】:

      您可以在for..of 循环中使用展开元素将数组值连接到单个数组

      let arr1 = [1,2,3];
      let arr2 = [3,4,5];
      let arr3 = [];
      
      for (let arr of [arr1, arr2 /* , arrN */]) arr3.push(...arr);
      
      console.log(arr3);

      【讨论】:

        【解决方案7】:

        您可以使用递归函数和Array.prototype.concat

        const concatN = (x,...xs) =>
          x === undefined ? [] : x.concat(concatN(...xs))
        
        console.log(concatN([1,2,3], [4,5,6], [7,8,9]))
        // [1,2,3,4,5,6,7,8,9]

        您可以使用reduceArray.prototype.concat 执行相同的操作。这与已接受的答案相似,但在这种情况下,x.concat(y) 完全可以接受(并且可能会更快)

        const concatN = (...xs) =>
          xs.reduce((x,y) => x.concat(y), [])
        
        console.log(concatN([1,2,3], [4,5,6], [7,8,9]))
        // [1,2,3,4,5,6,7,8,9]

        【讨论】:

          【解决方案8】:

          我们可以通过以下方式使用es6解决

          function mergeTwo(arr1, arr2) {
            let result = [...arr1, ...arr2];
            return result.sort((a,b) => a-b);
          }
          

          【讨论】:

            【解决方案9】:

            最好的选择是使用 FlatMap,它可以帮助我们将多个数组合并为一个数组。

            例子:

            let arrs = [[1, 2], [3, 4], [5, 6]];
            arrs.flatMap(a => a);
            

            结果是

            > (6) [1, 2, 3, 4, 5, 6]
            

            快乐编码...

            【讨论】:

              【解决方案10】:

              let arr1 = [1,2,3];
              let arr2 = [3,4,5];
              let arrs = [arr1, arr2].flat(); // [1,2,3,3,4,5]
              
              console.log(arrs);

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2019-07-13
                • 1970-01-01
                • 2016-03-04
                • 2015-12-27
                • 1970-01-01
                • 1970-01-01
                • 2017-08-28
                • 2021-07-02
                相关资源
                最近更新 更多