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
Add to Array jQuery
提问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
回答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.