开发者

Remove element from the end and add to the beginning

I tried this function :

var myArray=new Array("1", "2", "3", "4");
var value=开发者_Python百科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?


Use unshift method (reference)

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


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜