从数组中删除第一个元素并返回数组减去第一个元素

时间:2023-01-04 21:23:08

var myarray = ["item 1", "item 2", "item 3", "item 4"];

//removes the first element of the array, and returns that element.
alert(myarray.shift());
//alerts "item 1"

//removes the last element of the array, and returns that element.
alert(myarray.pop());
//alerts "item 4"

  1. How to remove the first array but return the array minus the first element
  2. 如何删除第一个数组但返回数组减去第一个元素
  3. In my example i should get "item 2", "item 3", "item 4" when i remove the first element
  4. 在我的例子中,当我删除第一个元素时,我应该得到“第2项”,“第3项”,“第4项”

3 个解决方案

#1


35  

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

正如其他人所建议的那样,你也可以使用slice(1);

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

#2


3  

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

#3


-1  

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

您可以使用array.slice(0,1)//删除第一个索引并返回数组。

#1


35  

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

正如其他人所建议的那样,你也可以使用slice(1);

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

#2


3  

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

#3


-1  

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

您可以使用array.slice(0,1)//删除第一个索引并返回数组。