Home  >  Article  >  Web Front-end  >  The usage and difference between some() and filter() in JavaScript arrays (with code)

The usage and difference between some() and filter() in JavaScript arrays (with code)

亚连
亚连Original
2018-05-21 13:49:472421browse

Now I will bring you an article on the usage and difference between JavaScript array some() and filter(). Now I share it with you and give it as a reference.

some method

array1.some(callbackfn[, thisArg])

For each element in array array1 Call the callback function callbackfn. When the callback function returns true or all arrays are traversed, the some method terminates. The optional parameter thisArg can replace the this object in the callback function

filter method

array1.filter(callbackfn[, thisArg])

Call the callback function callbackfn method on each element in array array1. This method will return a new collection of elements that return true in the callback function. The optional parameter thisArg can replace the this object in the callback function

The difference between the two

Some methods return a boolean value, which can be used Check whether there is an object in the array

The filter method returns a new array, which can be used to filter the objects in the array

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future. .

Related articles:

Sample code for creating fireworks special effects using p5.js_javascriptTips

Basic usage of Javascript

JavaScript dynamically changes HTML page elements such as adding or deleting

The above is the detailed content of The usage and difference between some() and filter() in JavaScript arrays (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn