從末尾刪除元素並添加到開頭

[英]Remove element from the end and add to the beginning


I tried this function :

我試過這個功能:

var myArray=new Array("1", "2", "3", "4");
var value=myArray.pop();
myArray.push(value);

but I'd like to remove 4 from the end and add it to the beginning. So should be 4 1 2 3. So a LIFO methodology using push and pop... How can I do it?

但我想從最后刪除4並將其添加到開頭。所以應該是4 1 2 3.所以使用push和pop的LIFO方法......我怎么能這樣做?

4 个解决方案

#1


1  

var myArray=new Array("1", "2", "3", "4");
var value=myArray.pop();
myArray.splice(0, 0, value);

#2


5  

Use unshift method (reference)

使用unshift方法(參考)

var myArray=new Array("1", "2", "3", "4");
var value=myArray.pop();
myArray.unshift(value);

#3


3  

You almost had it...

你幾乎擁有它......

This is how you do it:

這是你如何做到的:

var myArray=new Array("1", "2", "3", "4");
var value=myArray.pop();
myArray.unshift(value);

#4


3  

var myArray=new Array("1", "2", "3", "4");
myArray.unshift(myArray.pop());

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/08/30/d3e9d07b1d9ff72e6c9e94b78dcee34c.html



 
  © 2014-2022 ITdaan.com 联系我们: