I: Array traversal
Array.prototype.map: (mainly for its return value)
The map() method creates a new array, and the result is that each element in the array is the return value after calling the provided function once.
const numList = [1,2,3] // Multiply each item in the array by 3, and then return a new array const resultLis = numList.map(item=> item * 3) console.log('Old array:',numList) console.log('New array:',resultLis)
Array.prototype.forEach: (no value is returned, but the array will be processed)
forEach() Method executes the given function once for each element of the array.
const numList = [1,2,3] const newArray = [] numList.forEach(item => { newArray.push(item + 1) console.log(item+1) }) console.log(numList,newArray)
Array.prototype.filter: (filter value, return a new array of values that meet the filter conditions)
filter() Method creates a new array containing all the elements of the test implemented by the provided function.
const list = [1,2,3,4] // Filter values greater than 2 const filtered = list.filter(item => item > 2) console.log(list,filtered )
Array.prototype.some: (as long as one of the array meets the conditions, it returns true, otherwise it returns false)
some() Method tests whether at least one element in the array passes the provided function test
const list = [1,2,3,4,5] // Detects whether one of the values in the array is greater than 2 const result = list.some(item=> item > 2) console.log(result)
Array.prototype.every: (all values in the value will return true only if they meet the conditions; otherwise, it will return false)
every() Method to test whether all elements in an array can pass the test of a specified function
const list = [2,3,4] // Detects whether all elements in the array are greater than 3 const result = list.every(item=> item > 3) console.log(result)
Array.prototype.reduce: (process each element in turn and return the processed results)
reduce() Method executes a reducer function (executed in ascending order) provided by you for each element in the array
Personally, I think the reduce() method is a little higher
Syntax:
arr.reduce(callback,[initialValue])
reduce executes a callback function for each element in the array, Callback can receive four parameters:
callback:
1: Previousvalue = = > must be
2: Currentvalue (currently processed element) = = > required
3: Index (index of the currently processed element in the array) = = > optional
4: Array (array calling reduce) = = > optional
initialValue:
Note: as the first call The value of the first parameter of the callback function. If no initial value is provided, the first element in the array is used. Calling reduce on an empty array without an initial value will report an error.
Example 1: (accumulation, no initial value)
const list = [1,2,3,4] const result = list.reduce((pre,curr)=> pre + curr) console.log(result,'--')
Example 1_ 1: (cumulative, with initial value)
const list = [1,2,3,4] const result = list.reduce((pre,curr)=> pre + curr,10) console.log(result,'--')
Column 2: (accumulate the values in the object array, no initial value)
const list = [ {name:'super',age:18}, {name:'tiger',age:19}, ] const totalAge = list.reduce((pre,curr)=> pre.age + curr.age) console.log(totalAge)
Liezi 2_ 1: (accumulate the values in the object array without initial value)
const list = [ {name:'super',age:18}, {name:'tiger',age:19}, ] const totalAge = list.reduce((pre,curr)=> pre.age + curr.age,20) console.log(totalAge)
Example 3: (converting a two-dimensional array into a one-dimensional array is a bit overqualified)
const list = [1,2,[3,4],['superTiger_Y','Blog']] const flattenedList = list.reduce((pre,curr)=> pre.concat(curr),[]) console.log(flattenedList )
Example 4: (calculate the number of occurrences of each element in the array)
const names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'] const countNames = names.reduce((pre,curr)=> { curr in pre ? pre[curr]++ : pre[curr] = 1 return pre },{}) console.log(countNames)
Example 5: (classify object s by attributes)
const people = [ {name:'super',age:21}, {name:'tiger',age:22}, {name:'_Y',age:21}, ] function groupBy(objectArray,property){ return objectArray.reduce((pre,curr)=>{ const key = curr[property] if(!pre[key]){ pre[key] = [] } pre[key].push(curr) return pre },{}) } const result = groupBy(people ,'age') console.log(result)
Example 6 (object array element merging)
const friends = [{ name: 'Anna', books: ['Bible', 'Harry Potter'], age: 21 }, { name: 'Bob', books: ['War and peace', 'Romeo and Juliet'], age: 26 }, { name: 'Alice', books: ['The Lord of the Rings', 'The Shining'], age: 18 }]; const allBooks = friends.reduce((pre,curr)=>{ return [...pre,...curr.books] },['superTiger_y']) console.log(allBooks)
Array.prototype.reduceRight
The usage is the same as that of reduce, but the traversal order is opposite. Reduce is from left to right, and reducereight is from right to left
Array.prototype.indexof (find the element and return the location of the first occurrence, or - 1 if it does not exist)
The indexOf() method returns the first index of a given element that can be found in the array. If it does not exist, it returns - 1.
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison']; console.log(beasts.indexOf('bison'));
Array. Prototype. LastIndexOf (find the element and return the last position, or - 1 if it does not exist)
lastIndexOf() Method returns the index of the last specified element in the array. If it does not exist, it returns - 1.
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison']; console.log(beasts.lastIndexOf('bison'));
Array.prototype.find (return the value of the first element satisfying the condition)
find() Method returns the value of the first element in the array that satisfies the provided test function. Otherwise return undefined.
const people = [ {name:'super',age:20}, {name:'tiger',age:19}, {name:'_Y',age:22} ] const item= people.find(item => item.age === 19) console.log('Elements with an age of 19:',item) const item1= people.find(item => item.age === 100) console.log('Elements with an age equal to 100:',item1)
Array.prototype.findIndex (returns the index of the value of the first element satisfying the condition)
findIndex() Method returns the index of the array that satisfies the value of the first element of the provided test function. Otherwise - 1 is returned
const people = [ {name:'super',age:20}, {name:'tiger',age:19}, {name:'_Y',age:22} ] const index= people.findIndex(item => item.age === 19) console.log('Index of elements with an age of 19:',index) const index1= people.findIndex(item => item.age === 100) console.log('Index of elements with an age equal to 100:',index1)