Create Array object
//one
var aValues= new Array();
//two
var aValues=new Array(20);
//three
var aColors=new Array();
aColors [0]="red";
aColors[1]="green";
aColors[2]="blue";
//four
var aColors=new Array( "red","green","blue");
//five
var aColors=["red","green","blue"];
join && split
join: connection string
var aColors=["red","green","blue"];
alert(aColors.join(","));//outputs "red,green,blue"
alert(aColors.join ("-spring-"));//outputs "red-spring-green-spring-blue"
alert(aColors.join("]["));//outputs "red][green][blue "
split: Split string
var sColors="red,green,blue";
var aColors=sColors.split(",");//outputs ["red", "green", "blue"]
var redColors=aColors[0].split("");//outputs ["r", "e", "d"]
concat && slice
concat: append array
var aColors=["red","green","blue "];
var aColors2=aColors.concat("yellow","purple");
alert(aColors);//outputs ["red", "green", "blue"]
alert (aColors2);//outputs ["red", "green", "blue", "yellow", "purple"]
slice: Returns a new array with specific items
var aColors=["red","green","blue", "yellow","purple"];
var aColors2=aColors.slice(1);//outputs ["green","blue","yellow","purple"]
var aColors3=aColors. slice(1,4);//outputs ["green","blue","yellow"]
push && pop
Like the stack, Array provides push and pop methods. The push method is used to add one or more items at the end of the Array, and the pop method is used to delete the last array item, returning it as a function value
var stack=new Array;
stack.push("red");
stack.push("green");
stack .push("blue");
alert(stack);//outputs ["red","green","blue"]
var vItem=stack.pop();
alert(vItem );//outputs ["blue"]
alert(stack);//otputs ["red","green"]
shift && unshift
shift: delete from array The first item, use it as the function return value, unshift: put an item at the first position of the array, and then move the remaining items down one position
var aColors=["red","green","blue"];
var vItem=aColors.shift( );
alert(aColors);//outputs ["green","blue"]
alert(vItem);//outputs ["red"]
aColors.unshift("black");
alert(aColors);//outputs ["black","green","blue"]
reverse && sort
reverse: reverse the order of the array items, sort: press The values of the array items are arranged in ascending order (first call the toString() method to convert all values into strings)
var aColors=["blue","green","red"];
aColors.reverse();
alert(aColors);//outputs [" red","green","blue"]
aColors.sort();
alert(aColors);//outputs ["blue","green","red"]
Note:
var aColors=[3,32 ,2,5];
aColors.sort();
alert(aColors);//outputs [2,3,32,5]
This is because numbers are converted to strings and then compared by character code.
splice
splice: Insert the data item into the middle of the array
1. Used for deletion: Just declare two parameters, the first parameter is the first item to be deleted Position, the second parameter is the number of deleted items
var aColors=["red","green","blue","yellow"];
aColors.splice(0,2);
alert(aColors);//outputs ["blue", " yellow"]
2. Used for insertion: declare three or more parameters (the second parameter is 0) to insert the data into the specified position. The first parameter is the starting position. The two parameters are 0, and the third and above parameters are insertion items
var aColors=["red","green","blue","yellow"];
aColors.splice(2,0,"black","white");
alert(aColors );//outputs ["red","green","black","white","blue", "yellow"]
3. Used to delete and insert: declare three or the above parameters (the second parameter is not 0), you can insert the data into the specified position. The first parameter is the starting position, the second parameter is the number of items to be deleted, and the third and above parameters are insert Item
var aColors=["red","green" ,"blue","yellow"];
aColors.splice(2,1,"black","white");
alert(aColors);//outputs ["red","green", "black","white", "yellow"]