Array in JS although there is no function status so high, but also has a significant position, below I will combine this ES5 in some commonly used methods, and ES6 in some methods to do some explanation and practical use.

1, ES5 array common methods:

1. Loop traversal.

Let arr = [1,2,3] for(let I =0; i<arr.length; i++){ console.log(i) // 1 2 3 }Copy the code

ForEach: no return value. Break and continue are not allowed. Just call Func for each element.

ForEach (function(elem,index,array){function(elem,index,array){function(elem,index,array){ console.log(elem,index) // 1 0 2 1 3 2 })Copy the code

3, map: returns a new array with each element as the result of the Func call.

Let result = arr.map(function(val){val += 1 return val}) console.log(arr,result) // [1,2,3] [2,3,4]Copy the code

4. Filter: Returns an array of elements that match the Func condition.

Let result = arr.filter(function(val){return val == 2}) console.log(arr,result) // [1,2,3] [2]Copy the code

5, some: returns a Boolean value to determine whether any elements meet the Func condition (true if one does).

Let result = arr.some(function(val){return val == 2}) console.log(arr,result) // [1,2,3] trueCopy the code

6, every: returns a Boolean value for each element that satisfies the Func condition (true for all).

Every (function(val){return val == 2}) console.log(arr,result) // [1,2,3] falseCopy the code

7. Reduce: The receiver function acts as an accumulator

Let sum = arr.reduce(function(prev,cur,index,arr){  return prev + cur },0) console.log(sum) // 6Copy the code

7-2. Get the maximum value in the array.

Let arr = [1,2,3] let Max = arr.reduce(function(prev,cur){math.max (prev,cur)}) console.log(Max) // 3Copy the code

7-3. Deduplicate arrays

Let r = [1,2,3,3] let res = arr.reduce(function(prev,cur){prev.indexof (cur) == -1 && prev.push(cur) return prev},[]) The console. The log (res) / / [1, 2, 3]Copy the code

8, for… In: Functions below the prototype are traversed when traversing the array

Array.prototyp.foo = function(){console.log("foo")} let arr = [1,2,3] for(let index in arr){console.log(index) // Iterating through the array also iterates through the function foo} below the stereotypeCopy the code

1. Find: Returns the first element that passes the test

Find (function(val){return val > 2}) console.log(res) // 3Copy the code

FindIndex: returns the index of the first element that passed the test

Find (function(val){return val > 2}) console.log(res) // 2Copy the code

3, for… of

Let arr = [1,2,3,4] for(let item of arr){console.log(item)Copy the code

3-1, values: Only traversal values

let arr = ["a","b","c","d"]
for(let item of arr.values()){
    console.log(item) // "a" "b" "c" "d"
}
Copy the code

3-2, keys: Traversal index only

let arr = ["a","b","c","d"]
for(let item of arr.keys()){
    console.log(item) // 0 1 2 3 
}
Copy the code

“Entries” : goes through index and value

let arr = ["a","b","c","d"]
for(let item of arr.entries){
    console.log(item) // [0, "a"] [1, "b"] [2, "c"] [3, "d"]
}
Copy the code