Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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));

Why not use ES6?

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

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); 

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>

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!