How can I add new array elements at the beginning of an array in Javascript?

前端 未结 13 601
一生所求
一生所求 2020-11-22 13:31

I have a need to add or prepend elements at the beginning of an array.

For example, if my array looks like below:

[23, 45, 12, 67]

相关标签:
13条回答
  • 2020-11-22 14:23

    you have an array: var arr = [23, 45, 12, 67];

    To add an item to the beginning, you want to use splice:

    var arr = [23, 45, 12, 67];
    arr.splice(0, 0, 34)
    console.log(arr);

    0 讨论(0)
提交回复
热议问题