Array Creation
First type:
var colors = new Array();
var colors = new Array(20);//Create an array containing 20 items
var colors = new Array("Greg");//Create an array containing 1 item, which is a string Array of "Greg"
var colors = new Array("red","blue","green"); //Create 3 items
Second type:
var colors = ["red","blue","green"] ;
var colors = [];//Create an empty array
Note: The index of the array starts from 0
1. length attribute
length attribute The number of items in the array is saved in, such as:
var colors = ["red","blue","green"];
alert(colors.length); //3
The length attribute is not read-only, you can use the length attribute in the array Remove items at the end, or add new items, such as:
var colors = ["red","blue","green"];
colors.length = 2;
alert(colors); //red,blue
colors[colors.length] = "black";
alert(colors); //red, blue, black
2.join() method, connect the items in the array
var colors = ["red","blue","green"];
alert( colors.join(",")); //red,blue,green
alert(colors.join("||")); //red||blue||green
3. Array stack methods: push() and pop()
push() method can accept any number of parameters, add them to the end of the array one by one, and return the length of the modified array
pop() The method removes the last item from the end of the array, reduces the length value of the array, and returns the removed item
var colors = new Arrary(); //Create an array
var count = colors.push("red","green"); //Push two items to the end of the array
alert(count); //2
count = colors.push("black"); //Push an item to the end of the array
alert(count); //3
var item = colors.pop(); //Remove the last item and return the value
alert(item); //"black"
alert(count); //2
4. Array queue methods: push() and shift(), unshift()
push() method is the same as above
shift() method removes the first item in the array and returns it, and the length of the array is reduced 1 The
unshift() method adds any item to the front of the array and returns the length of the new array
var colors = new Arrary(); //Create an array
var count = colors.push("red","green"); //Push two items to the end of the array
alert(count); //2
count = colors.push("black"); //Push an item to the end of the array
alert(count); //3
var item = colors.shift(); //Remove the first item and return the value
alert(item); //"red"
alert(colors); //green,black
count = colors .unshift("blue"); //Push an item to the front of the array
alert(count); //3
alert(colors); //blue, green, black
5. Reordering methods: reverse() and sort()
reverse() method reverses the order of array items
sort() method defaults to sorting array items in ascending order by string size, and can accept a comparison size The function takes as parameter
var values = [1,2, 3,4,5];
values.reverse();
alert(values); //5,4,3,2,1
//Ascending sorting function
function compare(value1,value2) {
if (value1 < value2) {
return -1; //Descending order is changed to 1
} else if (value1 > value2) {
return 1; //Descending order changed to -1
} else {
return 0;
}
}
//Array sorted in ascending order
var values = [0, 1,5,15,20,10];
values.sort(compare);
alert(values);//0,1,5,10,15,20
//You can use this function for numerical types, ascending order
function compare(value1,value2) {
return value2 - value1;
}
6. Some methods of arrays: concat() method, slice() method and splice() method
The concat() method adds parameters to the end of the original array and returns a new array. The original array remains unchanged.
The slice() method returns the items in the array. If there is one parameter, it returns all items from the specified position to the end of the array. When two parameters are used, the items between the start position and the end position (excluding the end position) are returned, and the original array remains unchanged
The splice() method inserts, deletes, or replaces items in the array, and returns the deleted item (returns an empty array if not deleted), the original array changes
//concat() method
var colors = ["red","green","blue"];
var colors2 = colors.concat("yellow",["black","brown"] );
alert(colors); //red,green,blue
alert(colors2); //red,green,blue,yellow,black,brown
//slice() method
var colors = ["red"," green","blue","yellow","black"];
var colors2 = colors.slice(1); //With one parameter, return all items from the specified position to the end of the array
var colors3 = colors .slice(1,4); //If there are two parameters, return the items between the start position and the end position (excluding the end position)
alert(colors2); //green,blue,yellow,black
alert(colors3); //green,,blue,yellow
//splice() method
//Insert item, specify 3 parameters when inserting: starting position, 0 (item to be deleted), item to be inserted
var colors = ["red","green","blue"];
var inserted = colors.splice(1,0,"yellow","orange"); //Insert two items starting from position 1
alert(colors); //red,yellow,orange,green,blue
alert(inserted); //Empty array
//Replacement item, specify 3 parameters when deleting: starting position, Items to be deleted, any items to be inserted
var colors = ["red","green","blue"];
var replaced = colors.splice(1,1,"black","brown "); //Delete one item and insert two items
alert(colors); //red,black,browm,blue
alert(replaced); //green