Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add to Array jQuery

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...

like image 769
test Avatar asked May 02 '11 19:05

test


People also ask

How do you add an element to an array array?

When you want to add an element to the end of your array, use push(). If you need to add an element to the beginning of your array, try unshift(). And you can add arrays together using concat().

How do you add an element to an array in JavaScript?

JavaScript Array push() The push() method adds new items to the end of an array. The push() method changes the length of the array.

Can you append an array to an array in JavaScript?

To append one array to another, call the concat() method on the first array, passing it the second array as a parameter, e.g. const arr3 = arr1. concat(arr2) . The concat method will merge the two arrays and will return a new array.


2 Answers

For JavaScript arrays, you use push().

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

For jQuery objects, there's 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.

like image 191
Rocket Hazmat Avatar answered Oct 14 '22 23:10

Rocket Hazmat


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

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] 
like image 28
Darin Dimitrov Avatar answered Oct 14 '22 21:10

Darin Dimitrov