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);
精彩评论