从数组中删除第一个元素并返回数组减去第一个元素 [英] remove first element from array and return the array minus the first element

查看:129
本文介绍了从数组中删除第一个元素并返回数组减去第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< pre class =snippet-code-js lang-js prettyprint-override> var myarray = [item 1,item 2,item 3,item 4]; //删除数组的第一个元素,并返回该元素.alert(myarray.shift()); //提醒item 1//删除数组的最后一个元素,并返回该元素.alert(myarray.pop( )); //提醒第4项


  1. 如何删除第一个数组但返回数组减去第一个元素

  2. 在我的例子中我应该得到item 2,item 3 ,第4项当我删除第一个元素时


解决方案

这应该删除第一个元素,然后你可以返回剩余的:



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



和其他人一样建议你也可以使用slice(1);



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


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. In my example i should get "item 2", "item 3", "item 4" when i remove 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));

这篇关于从数组中删除第一个元素并返回数组减去第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆