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

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

    you can reverse your array and push the data , at the end again reverse it:

    var arr=[2,3,4,5,6];
    var arr2=1;
    arr.reverse();
    //[6,5,4,3,2]
    arr.push(arr2);
    
    0 讨论(0)
  • 2020-11-22 13:57

    If you want to push elements that are in a array at the beginning of you array use <func>.apply(<this>, <Array of args>) :

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

    0 讨论(0)
  • 2020-11-22 13:58

    Using splice we insert an element to an array at the begnning:

    arrName.splice( 0, 0, 'newName1' );
    
    0 讨论(0)
  • 2020-11-22 14:00

    Without Mutate

    Actually, all unshift/push and shift/pop mutate the origin array.

    The unshift/push add an item to the existed array from begin/end and shift/pop remove an item from the beginning/end of an array.

    But there are few ways to add items to an array without a mutation. the result is a new array, to add to the end of array use below code:

    const originArray = ['one', 'two', 'three'];
    const newItem = 4;
    
    const newArray = originArray.concat(newItem); // ES5
    const newArray2 = [...originArray, newItem]; // ES6+
    

    To add to begin of original array use below code:

    const originArray = ['one', 'two', 'three'];
    const newItem = 0;
    
    const newArray = (originArray.slice().reverse().concat(newItem)).reverse(); // ES5
    const newArray2 = [newItem, ...originArray]; // ES6+
    

    With the above way, you add to the beginning/end of an array without a mutation.

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

    Cheatsheet to prepend new element(s) into the array

    1. Array#unshift

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

    2. Array#splice

    const list = [23, 45, 12, 67];
    
    list.splice(0, 0, 34);
    
    console.log(list); // [34, 23, 45, 12, 67];

    3. ES6 spread...

    const list = [23, 45, 12, 67];
    const newList = [34, ...list];
    
    console.log(newList); // [34, 23, 45, 12, 67];

    4. Array#concat

    const list = [23, 45, 12, 67];
    const newList = [32].concat(list);
    
    console.log(newList); // [34, 23, 45, 12, 67];

    Note: In each of these examples, you can prepend multiple items by providing more items to insert.

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

    Quick Cheatsheet:

    The terms shift/unshift and push/pop can be a bit confusing, at least to folks who may not be familiar with programming in C.

    If you are not familiar with the lingo, here is a quick translation of alternate terms, which may be easier to remember:

    * array_unshift()  -  (aka Prepend ;; InsertBefore ;; InsertAtBegin )     
    * array_shift()    -  (aka UnPrepend ;; RemoveBefore  ;; RemoveFromBegin )
    
    * array_push()     -  (aka Append ;; InsertAfter   ;; InsertAtEnd )     
    * array_pop()      -  (aka UnAppend ;; RemoveAfter   ;; RemoveFromEnd ) 
    
    0 讨论(0)
提交回复
热议问题