1. The most traditional method is for loop
Var arr = 1 "first", "second", "third", "fourth", 3,5,8]; 2 for(var i = 0; i < arr.length; i++){ 3 console.log(arr[i]); 4}Copy the code
The for… in
1 var arr = [" first "and" second ", "third", "fourth", 3,5,8]; 2 for(var i in arr){ 3 console.log(arr[i]); 4}Copy the code
The for… of
1 var arr = [" first "and" second ", "third", "fourth", 3,5,8]; 2 for(var item of arr){ 3 console.log(item); 4}Copy the code
Although the for… In, for… Both of can be a series of calendar numbers, but there are still big differences between them.
The main difference between the two is their iterative approach
- It is recommended to use for in when looping over object properties and for of when iterating over groups of numbers
- The for… In loop is key, for… The “of” loop is value
- The for… In is ES5 standard, for… Of is an ES6 standard. There may be some compatibility problems. Please use it carefully
- The for… Of does not iterate over ordinary objects and needs to be used with object.keys ()
2. Foreach method: The function passed to foreach is executed once on each element of the array, passing the element as an argument to the function
Var arr = 1 "first", "second", "third", "fourth", 3,5,8]; 3 arr. ForEach (function(element,index){4 console.log(element); 5 6})Copy the code
Note: unassigned values are not iterated through the foreach loop, but elements that are manually assigned to undefined are listed
3. Map iterates through the array, performs operations on array elements through callback, puts all operation results into the array, and returns the array
1 var arr = ["first","second",'third' ,"fourth"]; 2 var arr2 = arr.map(function(item){ 3 return item.toUpperCase(); 4 }) 5 console.log(arr2); 6 // output: 7 [FIRST,SECOND,THIRD, FOURTH]Copy the code
4. Filter () returns a new array containing all elements that return true on the callback function. The callback function acts as a filter, which returns true on elements and conditions, and filter returns all elements that match the criteria
1 var arr = [" first "and" second ", "third", "fourth", 3,5,8]; 2 var arr3 = arr.filter(function(item){ 3 if(typeof item == 'number'){ 4 return item; 5 } 6 }) 7 console.log(arr3); 8 // output result: 9 [3,5,8]Copy the code
5. Find () returns the value of the first element of the array that passes the test
When an element in an array returns true on a test condition, find() returns the element that matches the condition, and the subsequent value is not called. Returns undefined if there are no eligible elements
Var arr = [1,2,3,4,5,6,7]; 2 var newArr = arr.find(function(elem){ 3 return elem>5; 4}); 5 console.log(newArr); 6 // Output 7 6Copy the code
Note :find() is not executed for an empty array. Find () does not change the original value of the array.
6. Every () returns true when each element in the array is returned true on the callback.
Every () differs from filter() in that the latter returns all elements that match the filter criteria; The former determines whether all elements in the array meet the criteria and returns a Boolean value
1 var arr = [" first "and" second ", "third", "fourth", 3,5,8]; 2 var bol = arr.every(function(element){ 3 if(typeof element == 'string'){ 4 return element; 5 } 6 }) 7 console.log(bol); //falseCopy the code
7. Some () returns true as long as an item in the array is on the callback
The difference between every() and some() is that every() requires all elements to be true, while some() requires all elements to be true
1 var arr = [" first "and" second ", "third", "fourth", 3,5,8]; 2 var bol = arr.some(function(element){ 3 if(typeof element == 'string'){ 4 return element; 5 } 6 }) 7 console.log(bol); //trueCopy the code
8. Findindex () ES6 array new API, find the index that matches the criteria and return
1 var ages = [3, 10, 18, 20]; 2 3 function checkAdult(age) { 4 return age >= 2; 5 } 6 7 const bol = ages.findindex(checkAdult) 8 9 console.log(bol); / / 2Copy the code
Source: Blogland