[Front-end interview questions] [JavaScript] What are the looping methods for arrays? What is the role of each?

Q: What kinds of loops are there in an array? What is the role of each?

A:

  1. every() will filter the conditions and return a boolean value
  2. some() will filter the conditions and return a boolean value
  3. filter() will filter the qualified elements and summarize the elements as required, and return a new array, which will change the previous array
  4. forEach() will traverse each element in the array

reference:

The every () method tests whether all elements in an array pass the test of a specified function. It returns a boolean value.

The filter () method creates a new array containing all the elements that pass the tests fulfilled by the provided function.

The forEach () method executes the provided function once for each element of the array.

The some() method tests whether at least one element is available via the provided function method. This method returns a Boolean type

Supplementary knowledge:

every(): One false is false , all must return true to return true, even if there is one false, it will return false

some():   One true is true , as long as one of them is true, it will return true

Guess you like

Origin blog.csdn.net/weixin_56035334/article/details/126538848