Javascript 将多个元素推送到数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14723848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:44:31  来源:igfitidea点击:

push multiple elements to array

javascript

提问by evfwcqcg

I'm trying to push multiple elements as one array, but getting error

我试图将多个元素作为一个数组推送,但出现错误

> a = []
[]
> a.push.apply(null, [1,2])
TypeError: Array.prototype.push called on null or undefined

I'm trying to do similar stuff that I'd do in ruby, I was thinking that applyis something like *.

我正在尝试做我在 ruby​​ 中做的类似的事情,我想那apply是类似*.

>> a = []
=> []
>> a.push(*[1,2])
=> [1, 2]

采纳答案by Niet the Dark Absol

When using most functions of objects with applyor call, the contextparameter MUST be the object you are working on.

使用apply或使用对象的大多数功能时callcontext参数必须是您正在处理的对象。

In this case, you need a.push.apply(a, [1,2])(or more correctly Array.prototype.push.apply(a, [1,2]))

在这种情况下,您需要a.push.apply(a, [1,2])(或更准确地说Array.prototype.push.apply(a, [1,2])

回答by amit1310

You can push multiple elements into an array in the following way

您可以通过以下方式将多个元素推送到数组中

var a = [];
    
a.push(1, 2, 3);

console.log(a);

回答by canac

Now in ECMAScript2015 (a.k.a. ES6), you can use the spread operatorto append multiple items at once:

现在在 ECMAScript2015(又名 ES6)中,您可以使用扩展运算符一次附加多个项目:

var arr = [1];
var newItems = [2, 3];
arr.push(...newItems);
console.log(arr);

See Kangax's ES6 compatibilitytable to see what browsers are compatible

查看Kangax的ES6兼容性表,查看兼容哪些浏览器

回答by VisioN

You can use Array.concat:

您可以使用Array.concat

var result = a.concat(b);

回答by John

If you want an alternative to Array.concatin ECMAScript 2015 (a.k.a. ES6, ES2015) that, like it, does not modify the array but returns a new array you can use the spread operatorlike so:

如果你想要一个Array.concatECMAScript 2015(又名 ES6、ES2015)的替代品,它不会修改数组而是返回一个新数组,你可以像这样使用扩展运算符

var arr = [1];
var newItems = [2, 3];
var newerItems = [4, 5];
var newArr = [...arr, ...newItems, ...newerItems];
console.log(newArr);

Note this is different than the pushmethod as the pushmethod mutates/modifies the array.

请注意,这与push方法不同,因为push方法会改变/修改数组。

If you want to see if certain ES2015 features work in your browser check Kangax's compatibility table.

如果您想查看某些 ES2015 功能是否在您的浏览器中工作,请查看Kangax 的兼容性表

You can also use Babelor a similar transpiler if you do not want to wait for browser support and want to use ES2015 in production.

如果您不想等待浏览器支持并希望在生产中使用 ES2015,您也可以使用Babel或类似的转译器。

回答by 12kb

There are many answers recommend to use: Array.prototype.push(a, b). It's nice way, BUTif you will have really big b, you will have stack overflow error (because of too many args). Be careful here.

有很多答案推荐使用:Array.prototype.push(a, b). 这是一个很好的方法,但是如果你的 b 真的很大,你就会有堆栈溢出错误(因为参数太多)。这里要小心。

See What is the most efficient way to concatenate N arrays?for more details.

请参阅连接 N 个数组的最有效方法是什么?更多细节。

回答by Alex Kumbhani

var a=[];
a.push({
 name_a:"abc",
 b:[]
});

a.b.push({
  name_b:"xyz"
});