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

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

    If you need to continuously insert an element at the beginning of an array, it is faster to use push statements followed by a call to reverse, instead of calling unshift all the time.

    Benchmark test: http://jsben.ch/kLIYf

    0 讨论(0)
  • 2020-11-22 14:18

    Using ES6 destructuring: (avoiding mutation off the original array)

    const newArr = [item, ...oldArr]

    0 讨论(0)
  • 2020-11-22 14:19

    Use unshift. It's like push, except it adds elements to the beginning of the array instead of the end.

    • unshift/push - add an element to the beginning/end of an array
    • shift/pop - remove and return the first/last element of an array

    A simple diagram...

       unshift -> array <- push
       shift   <- array -> pop
     
    

    and chart:

              add  remove  start  end
       push    X                   X
        pop           X            X
    unshift    X             X
      shift           X      X
    

    Check out the MDN Array documentation. Virtually every language that has the ability to push/pop elements from an array will also have the ability to unshift/shift (sometimes called push_front/pop_front) elements, you should never have to implement these yourself.


    As pointed out in the comments, if you want to avoid mutating your original array, you can use concat, which concatenates two or more arrays together. You can use this to functionally push a single element onto the front or back of an existing array; to do so, you need to turn the new element into a single element array:

    const array = [3, 2, 1]
    
    const newFirstElement = 4
    
    const newArray = [newFirstElement].concat(array) // [ 4, 3, 2, 1 ]
    
    console.log(newArray);

    concat can also append items. The arguments to concat can be of any type; they are implicitly wrapped in a single-element array, if they are not already an array:

    const array = [3, 2, 1]
    
    const newLastElement = 0
    
    // Both of these lines are equivalent:
    const newArray1 = array.concat(newLastElement) // [ 3, 2, 1, 0 ]
    const newArray2 = array.concat([newLastElement]) // [ 3, 2, 1, 0 ]
    
    console.log(newArray1);
    console.log(newArray2);

    0 讨论(0)
  • 2020-11-22 14:20

    With ES6 , use the spread operator ... :

    DEMO

    var arr = [23, 45, 12, 67];
    arr = [34, ...arr]; // RESULT : [34,23, 45, 12, 67]
    
    console.log(arr)

    0 讨论(0)
  • 2020-11-22 14:20

    Another way to do that through concat

    var arr = [1, 2, 3, 4, 5, 6, 7];
    console.log([0].concat(arr));

    The difference between concat and unshift is that concat returns a new array. The performance between them could be found here.

    function fn_unshift() {
      arr.unshift(0);
      return arr;
    }
    
    function fn_concat_init() {
      return [0].concat(arr)
    }
    

    Here is the test result

    0 讨论(0)
  • 2020-11-22 14:22

    array operations image

    var a = [23, 45, 12, 67];
    a.unshift(34);
    console.log(a); // [34, 23, 45, 12, 67]

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