Add to Array jQuery

JavascriptJquery

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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]

Solution 3 - Javascript

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

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

Solution 4 - Javascript

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

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.

Solution 5 - Javascript

just it jquery

var linkModel = {
            Link: "",
            Url: "",
            Summary: "",
        };

var model = [];
for (let i = 1; i < 2; i++) {
    linkModel.Link = "Test.com" + i;
    linkModel.Url= "www.Test.com" + i;
    linkModel.Summary= "Test is add" + i;
    model.Links.push(linkModel);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontestView Question on Stackoverflow
Solution 1 - JavascriptRocket HazmatView Answer on Stackoverflow
Solution 2 - JavascriptDarin DimitrovView Answer on Stackoverflow
Solution 3 - JavascriptsholsingerView Answer on Stackoverflow
Solution 4 - JavascriptsarojView Answer on Stackoverflow
Solution 5 - Javascriptihsan güçView Answer on Stackoverflow