什么是连接ñ数组在JavaScript中最有效的方法是什么? [英] What is the most efficient way to concatenate N arrays in JavaScript?

查看:132
本文介绍了什么是连接ñ数组在JavaScript中最有效的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是连接对象的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.

推荐答案

如果你串联两个以上的阵列,<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat\"><$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()不过的there似乎是从这样没有性能优势。

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中最有效的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆