Append object to array in JavaScript

Best ways to append an array of objects.


Libraries

Code Snippet
// Pure JavaScript: Array.prototype.push
myArray.push("Hello");


// Pure JavaScript: Array.prototype.concat
myArray = myArray.concat([
    "Hallo",
    "Hola",
    "Bonjour"
]);


// Pure JavaScript: Array.prototype.apply
Array.prototype.push.apply(myArray, ["Hi",
                                     "Hey",
                                     "Salut"
]);


Implementation

Basically <code>Array.prototype.push(ObjectToAppend);</code> appends one object. The functions <code>Array.prototype.concat(ArrayToAppend); Array.prototype.apply(basicArray, ArrayToAppend);</code> combines both arrays.





Comments
  • mode_editWrite a comment



  • forumComments




Embed Code

We provide special containers which include the full snippet and a link to this site. So for example when you make a tutorial and you are using code from this site, you can use one of the following snippet embeddings:

Load Preview Get Code Close Preview







    Share
    Rating
    thumb_up 1 thumb_down
    Author
    Thorolus

    I'm the head developer and co-founder of GlovilGames Studios (glovilgames.com) and other platforms GlovilGames created, like vsnippets.com, worth-calculator.net, python-obfuscator.com and some mobile apps and games.

    Tags
    Append
    Array



    Informations

    posted on 2016-09-14 15:51:34

    viewed 138 times

    snippet's UUID is 57d955e65e470

    Similar Snippets