【问题标题】:Merge Array of Arrays | NodeJS合并数组 |节点JS
【发布时间】:2019-07-02 01:20:15
【问题描述】:

我想知道如何合并数组数组。例如,我有以下数组:

[ [ {marca: 'fiat', modelo: 'palio'} ], [ {marca: 'nissan', modelo: 'march'} ] ]

我想得到这个数组,合并上面的数组后:

[ {marca: 'fiat', modelo: 'palio'}, {marca: 'nissan', modelo: 'march'} ]

谢谢,伙计们,我很感激。

【问题讨论】:

    标签: javascript arrays node.js merge


    【解决方案1】:

    您可以将ES6-spread operator 与 Array.reduce 结合使用,例如

    const arr = [ [ {marca: 'fiat', modelo: 'palio'} ], [ {marca: 'nissan', modelo: 'march'} ] ];
    console.log(arr.reduce((a, b) => [...a, ...b], [])); 

    【讨论】:

      【解决方案2】:

      您可以尝试新的但仍处于试验阶段的 flat() 数组方法...

      const input = [
        [ {marca: 'fiat', modelo: 'palio'} ],
        [ {marca: 'nissan', modelo: 'march'} ]
      ];
      
      console.log(input.flat());

      或者像这样使用reduce()

      const input = [
        [ {marca: 'fiat', modelo: 'palio'}, {marca: 'fiat', modelo: 'fiesta'} ],
        [ {marca: 'nissan', modelo: 'march'} ]
      ];
      
      let res = input.reduce((acc, curr) => acc.push(...curr) && acc, []);
      
      console.log(res);

      【讨论】:

      • 我想指出,第二个示例的输入可能不遵循 Vinicius Martins 建立的“标准”,因为 Fiat Fiesta 项目有一个名为“model”的属性,它的命名与其他物品''modelo',这是一个葡萄牙语单词,意思是,呃,模型。巴西人:)
      • @Gustavo6046 我来自阿根廷,这里也是modelo。我的错,对不起!现在更新了!
      • 没关系!错误发生。我想当我发表那条评论时,我太关注细节了。 :P
      【解决方案3】:

      有很多方法可以做到这一点。假设您的输入数据是这样的,输入数组的每个项目都是一个数组本身,其中一个项目,Array#map 的使用就足够了:

      const input = [
        [{
          marca: 'fiat',
          modelo: 'palio'
        }],
        [{
          marca: 'nissan',
          modelo: 'march'
        }]
      ];
      
      /* 
      For each subArray of input array where the subArray
      is known to have a single element, extract that single
      element and return it as the result of each map iteration 
      */
      const output = input.map(([item]) => item)
      
      console.log(output)

      或者,对于“展平”具有可变长度的数组项数组的更通用的解决方案,您可以通过Array#reduce 实现,如下所示:

      const input = [
        [{
          marca: 'fiat',
          modelo: 'palio'
        }, {
          test: 'foo'
        }],
        [{
          marca: 'nissan',
          modelo: 'march'
        }]
      ];
      
      const output = input.reduce((finalArray, subArray) => {
        
        /* 
        For each item being iterated during reduce, concat
        the subArray to the finalArray 
        */
        return finalArray.concat(subArray);
      
      }, [])
      
      console.log(output)

      【讨论】:

        【解决方案4】:

        记录一下,如果你使用的是NodeJs,有一个名为array-flatten的包,你可以用它来安装

        npm install array-flatten --save
        

        例如:

        var flatten = require('array-flatten')
        
        flatten([1, [2, [3, [4, [5], 6], 7], 8], 9])
        //=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
        

        https://www.npmjs.com/package/array-flatten

        【讨论】:

          【解决方案5】:

          有几种方法可以做到这一点,我推荐Reduce,因为不需要时不需要安装 npm 包。

          let arrays = [
            [{
              marca: 'fiat',
              modelo: 'palio'
            }],
            [{
              marca: 'nissan',
              modelo: 'march'
            }]
          ]
          
          arrays = arrays.reduce((a, b) => a.concat(b), []);
          
          console.log(arrays);

          【讨论】:

            【解决方案6】:

            我使用了 lodash 的 flattenDeep 方法来解决它。 例如:

            _.flattenDeep(Array)
            

            谢谢大家的回答。

            【讨论】:

              【解决方案7】:

              在实现的地方,可以使用标准的Array's flat方法:

              let arr = [ [ {marca: 'fiat', modelo: 'palio'} ], [ {marca: 'nissan', modelo: 'march'} ] ];
              
              console.log(arr.flat());
              

              作为替代方案,如果它只是一个浅平面,您可以使用concat 和扩展运算符获得相同的结果:

              console.log([].concat(...arr));
              

              【讨论】:

                猜你喜欢
                • 2021-04-19
                • 2019-03-07
                • 1970-01-01
                • 2012-12-04
                • 1970-01-01
                • 2021-07-15
                • 1970-01-01
                • 1970-01-01
                • 2019-02-14
                相关资源
                最近更新 更多