Javascript 添加到数组 jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5861859/
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-23 19:14:04  来源:igfitidea点击:

Add to Array jQuery

javascriptjquery

提问by test

I know how to initliaize one but how do add I items to an Array? I heard it was push()maybe? I can't find it...

我知道如何初始化一个但如何将我的项目添加到数组中?听说是push()吧?我找不到它...

回答by Rocket Hazmat

For JavaScript arrays, you use push().

对于 JavaScript 数组,您使用push().

var a = [];
a.push(12);
a.push(32);

For jQuery objects, there's add().

对于 jQuery 对象,有add().

$('div.test').add('p.blue');

Note that while push()modifies the original array in-place, add()returns a new jQuery object, it does not modify the original one.

请注意,push()在原地修改原始数组时,会add()返回一个新的 jQuery 对象,但不会修改原始数组。

回答by Darin Dimitrov

pushis a native javascript method. You could use it like this:

push是一种原生的 javascript 方法。你可以这样使用它:

var array = [1, 2, 3];
array.push(4); // array now is [1, 2, 3, 4]
array.push(5, 6, 7); // array now is [1, 2, 3, 4, 5, 6, 7]

回答by sholsinger

You are right. This has nothing to do with jQuery though.

你是对的。不过,这与 jQuery 无关。

var myArray = [];
myArray.push("foo");
// myArray now contains "foo" at index 0.

回答by saroj

For JavaScript arrays, you use Both push() and concat() function.

对于 JavaScript 数组,您同时使用 push() 和 concat() 函数。

var array = [1, 2, 3];
array.push(4, 5);         //use push for appending a single array.




var array1 = [1, 2, 3];
var array2 = [4, 5, 6];

var array3 = array1.concat(array2);   //It is better use concat for appending more then one array.