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

前端 未结 13 604
一生所求
一生所求 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 13:57

    If you want to push elements that are in a array at the beginning of you array use .apply(, ) :

    const arr = [1, 2];
    arr.unshift.apply(arr, [3, 4]);
    console.log(arr); // [3, 4, 1, 2]

提交回复
热议问题