remove first element from array and return the array minus the first element

后端 未结 5 1377
温柔的废话
温柔的废话 2021-01-31 01:09



        
相关标签:
5条回答
  • 2021-01-31 01:26

    Try this

        var myarray = ["item 1", "item 2", "item 3", "item 4"];
    
        //removes the first element of the array, and returns that element apart from item 1.
        myarray.shift(); 
        console.log(myarray); 
    
    0 讨论(0)
  • 2021-01-31 01:29

    This should remove the first element, and then you can return the remaining:

    var myarray = ["item 1", "item 2", "item 3", "item 4"];
        
    myarray.shift();
    alert(myarray);

    As others have suggested, you could also use slice(1);

    var myarray = ["item 1", "item 2", "item 3", "item 4"];
      
    alert(myarray.slice(1));

    0 讨论(0)
  • 2021-01-31 01:32

    This can be done in one line with lodash _.tail:

    var arr = ["item 1", "item 2", "item 3", "item 4"];
    console.log(_.tail(arr));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

    0 讨论(0)
  • 2021-01-31 01:33

    Why not use ES6?

     var myarray = ["item 1", "item 2", "item 3", "item 4"];
     const [, ...rest] = myarray;
     console.log(rest)

    0 讨论(0)
  • 2021-01-31 01:38

    You can use array.slice(0,1) // First index is removed and array is returned.

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