问题描述
什么是连接对象的N个数组在的JavaScript
?
What is the most efficient way to concatenate N arrays of objects in JavaScript
?
数组是可变的,并且其结果可以被存储在所述输入阵列中的一个。
The arrays are mutable, and the result can be stored in one of the input arrays.
推荐答案
如果你串联两个以上的阵列,<$c$c>concat()是去了方便和可能的表现的方式。
If you're concatenating more than two arrays, concat()
is the way to go for convenience and likely performance.
var a = [1, 2], b = ["x", "y"], c = [true, false];
var d = a.concat(b, c);
console.log(d); // [1, 2, "x", "y", true, false];
有关串联只是两个数组,使用 push.apply()
可代替用于添加从一个阵列到另一个端部件的情况下,不会产生新的数组。随着片()
它也可以用来代替 CONCAT()
不过的没有性能优势。
For concatenating just two arrays, using push.apply()
can be used instead for the case of adding elements from one array to the end of another without producing a new array. With slice()
it can also be used instead of concat()
but there appears to be no performance advantage from doing this.
var a = [1, 2], b = ["x", "y"];
a.push.apply(a, b);
console.log(a); // [1, 2, "x", "y"];
这篇关于什么是连接ñ数组在JavaScript中最有效的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!