javascript pushing element at the beginning of an array

This question already has an answer here:

  • How can I add new array elements at the beginning of an array in JavaScript? 6 answers

  • 使用unshift ,它通过将参数添加到开头来修改现有数组:

    TheArray.unshift(TheNewObject);
    

    Use .unshift() to add to the beginning of an array.

    TheArray.unshift(TheNewObject);
    

    See MDN for doc on unshift() and here for doc on other array methods.

    FYI, just like there's .push() and .pop() for the end of the array, there's .shift() and .unshift() for the beginning of the array.


    对于一个丑陋版本的unshift使用splice

    TheArray.splice(0, 0, TheNewObject);
    
    链接地址: http://www.djcxy.com/p/19348.html

    上一篇: 将信息推送到数组,但在索引0处

    下一篇: 在数组的开头部署JavaScript元素