从末尾删除元素并添加到开头

时间:2021-01-21 17:38:48

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

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