Add to jQuery Array

I know how to initialize, but how do I add elements to the array? I heard that it was push() maybe? I can not find him...

+78
javascript jquery
May 02 '11 at 19:58
source share
4 answers

For JavaScript arrays, you use push() .

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

For jQuery objects, there is add() .

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

Please note: while push() changes the original array in place, add() returns a new jQuery object, it does not change the original.

+242
May 02 '11 at 20:01
source share

push is a native javascript method. You can use it as follows:

 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] 
+24
May 2 '11 at 19:59
source share

You're right. However, this has nothing to do with jQuery.

 var myArray = []; myArray.push("foo"); // myArray now contains "foo" at index 0. 
+10
May 02 '11 at 20:00
source share

For JavaScript arrays, you use both the push () and concat () functions.

 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. 
0
Jun 27 '17 at 13:55 on
source share



All Articles