This is my third article on getting Started

background

One of the most important aspects of front-end development is data processing, and arrays are the most easily encountered of all data types, so it’s important to deal with arrays. Here are some ways to navigate through arrays, and you’ll be on your way

Filter () traverses the group

  • Filter iterates through the array and returns a new array
  • The number of loops is the length of the array
let arr = [1.2.3.4.5]
let newArr = arr.filter((item,index) = > {
	return item>3
})
consolo.log(newArr)
// result: [4,5]
Copy the code

Some () traverses the array

  • Return true if the condition is met, false otherwise
  • The number of loops is less than or equal to the length of the array
  • It terminates the loop when the condition is met and returns true. If no item satisfies the condition, it iterates through the array and returns false
let arr = [1.2.3.4.5]
let newArr = arr.some((item,index) = > {
	return item>3
})
console.log(newArr)
/ / result: [true]

Copy the code

Map () traverses the array

  • It’s going to loop through the array
  • You get a new array, and what are the items in the returned array depending on the method called in the map function
let arr=[1.2.3.4.5.6]
let newArr = arr.map((item,index) = > {
	return item === 1
})
console.log(newArr)
// result: [2,3,4,5,6,7]
Copy the code

ForEach () traverses the array

  • Like a normal for loop, you can change the contents of the original array
  • A return is invalidated in forEach because forEach internally encapsulates a callback
let a = [1.2.3]
a.forEach((item,index) = > {
	a[index] = item + 1
})
console.long(a)
// result: [2,3,4]
Copy the code

find()

  • For an object, if there is a return value, the item that meets the condition is returned. Otherwise, undefined is returned
  • The number of loops is less than or equal to the array length
b=[{a:1}, {b:2}, {c:3}]
b.find((item,index) = > {return item.b===2})
// Result: {b:2}
Copy the code

findIndex()

  • Return the index of the item that meets the criteria, or -1 if not
  • The number of loops is less than or equal to the array length
b=[{a:1}, {b:2}, {c:3}]
b.findIndex((item,index) = > {return item.b===2})
// Result: 1
Copy the code

every()

  • This is used to check if all items match a condition, returning true if they do, false if they don’t, and terminating the traversal
  • The number of loops is less than or equal to the array length
a=[2.2.2.2.3]
a.every((item,index) = > {
	return item===2
})
// Result: false
Copy the code

Conclusion: if there is anything wrong or everyone wants to add to the welcome comments pointed out, everyone progress together, refueling!!