lodash合并n个数组

时间:2017-10-23 13:40:03

标签: javascript lodash

我想将n个数组合并为一个。

例如



sudo apt-get install gcc-4.9 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 gcc-4.9 : Depends: cpp-4.9 (= 4.9.4-2ubuntu1~14.04.1) but it is not going to be installed
           Depends: libgcc-4.9-dev (= 4.9.4-2ubuntu1~14.04.1) but it is not going to be installed
 munge : Depends: libmunge2 (= 0.5.11-1ubuntu1) but 0.5.11-1ubuntu1.1 is to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).




我尝试了许多其他类似的问题,但没有任何对我有用。请帮我举个例子。

2 个答案:

答案 0 :(得分:1)

考虑到要合并public class GunImp implements Weapon { public void set(String weapon) { this.weaponName = "ice "+weapon; this.bullet = "need bullet"; } public String getWeapon() { return this.weaponName; } public String getSubWeapon() { return this.bullet; } } 数组,可以使用包含这些数组的数组,并使用concatreduce合并它们:

n

答案 1 :(得分:0)

您可以使用concat和ES6传播语法。



var arr1 = [{name:'abc', age:34},{name:'xyz', age:44},{name:'fng', age:54}]
var arr2 = [{name:'dgc', age:54}]

var r = [].concat(...arr1, ...arr2);
console.log(r)




或者你可以创建带有任意数量数组并返回新数组的函数。



var arr1 = [{name:'abc', age:34},{name:'xyz', age:44},{name:'fng', age:54}]
var arr2 = [{name:'dgc', age:54}]

let merge = (...arr) => [].concat(...arr);
console.log(merge(arr1, arr2))