The Array type in JavaScript is often used, and the Array type also provides many methods to achieve our needs. Let's summarize it below.
1. Methods to create an Array
var colors=new Array();
var colors=new Array(3);//Create an array of length 3
var colors=new Array("red","blue")//Create an array["red","blue"]
Of course, the above new can be omitted and written, such as var colors=Array("red");
2. Directly use array literals
var colors=["red","blue","green"];
2. Array method 1
var colors=["red","blue","green"];
1. Get the length of the array colors.length;//3
2. Access the second item of the array colors[1];//blue
3. Change the second item of data colors[1]="black";//["red","black","green"]
4. Check whether it is an array colors instance of Array;//true
5. colors.toString();//The output strings are separated by commas by commas.
6. colors.join("|");//Customize the output string red|blue|green with "|"
7. colors.push("brown")//Add an item to the end of the array
8. colors.pop()//Delete an item to the end of the array
9. colors.shift()//Delete the first item of the array and get the value
10. colors.unshift("k1","k2")//Insert these two items in front of the array; ["k1","k2","red","blue","green"];
11. colors.reverse()//The order of flip arrays
12. colors.sort() or colors.sort([func]);
13. concat() returns a new array, without affecting the original array colors.concat() or colors.concat("k1");
14. slice(begin,end) Copy this data from the array subscript begin to end, excluding subscript end. If it is slice(begin), then it starts from subscript begin to the end of the array.
15. splice
splice(0,2)//Drop two items of the array starting from subscript 0
splice(2,0,"k1","k2") deletes 0 items from subscript 2, and then inserts two items from here.
splice(2,1,"k1")//Delete an item from subscript 2, and then insert an item from here
16. indexOf("item")//From the array header to find an item, return the index value after finding it, but cannot find it and return -1
17. lastIndexOf("item")//From the end of the array to find an item, return the subscript value after finding it, and return -1 if it cannot be found.
3. Array method 2: Iterative method (ECMAScript5)
1. Every(): Run the given function on each item of the array. Each item returns true, and then true (does not affect the original array)
var numbers=[1,2,3,2,1];//Judge whether each number is greater than 2var flag=numbers.every(function (item,index,array) { return item>2;});2. filter(): Run the given function on each item in the array and return the item with the function true (does not affect the original array)
var numbers=[1,2,3,2,1];//Return the item greater than 2 var array=numbers.filter(function (item,index,array) { return item>2;});3. forEach(): executes a given function on each item in the array, without returning the value (does not affect the original array)
var numbers=[1,2,3,2,1];//Output the square numbers of each item.forEach(function (item,index,array) { console.log(item*2);});4. map(): Execute a given function on each item of the array, and return an array composed of the result after each function call (does not affect the original array)
var numbers=[1,2,3,2,1];//Return the square of each item var array=numbers.map(function (item,index,array) { return item*item;});5. Some(): Execute the given function on each item in the array. If there is an item that returns true, it returns true.
var numbers=[1,2,3,2,1];var flag=numbers.some(function (item,index,array) { return item>2});3. Array method 3: Merge method (ECMAScript5)
1. The reduce() method starts from the first line of the array and traverses one by one to the end.
2. The reduceRight() method starts from the last item of the array and traverses forward one by one.
var numbers=[1,2,3,4,5];var result=numbers.reduce(function (prev,cur,index,array) { //prev: The result of the previous operation, which was the first term of the number at the beginning //cur: the current term of the array //index: the subscript of the current array //array: the array that performs this operation, currently numbers console.log("prev:"+prev); console.log("cur:"+cur); console.log("index:"+index); console.log("array:"+array); console.log("=============================================================================================================================================================================================================================================================================================================================================================The above JavaScript Array method summary (recommended) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.