What’s a way to append a value to an array?

This question already has an answer here:

  • How to append something to an array? 25 answers

  • Make a new array.

    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    

    And then push values like this

    fruits.push("Kiwi");
    

    You can do this:

    // create a new array, using the `literals` instead of constructor
    var array = [];
    
    // add a value to the last position + 1 (that happens to be array.length) 
    array[array.length] = 10;
    

    or

     // use the push method from Array.
     array.push(10);
    

    Also, if you have an Object and you want it to behave like an array (not recommended), you can use

    Array.prototype.push.call(objectLikeArray, 10);
    
    链接地址: http://www.djcxy.com/p/17940.html

    上一篇: 在JavaScript中将值推送到关联数组中

    下一篇: 什么是将值附加到数组的方法?