Detailed explanation of the differences between js array find, some, filter, and reduce

Detailed explanation of the differences between js array find, some, filter, and reduce

Distinguish the differences between the filter, find, some, and reduce methods in Array, and better apply them in daily coding according to their usage scenarios.

Array.find

Array.find returns an object (the first object that meets the condition) and stops traversing

const arrTest = [
    { id: 1, name: "a" },
    { id: 2, name: "b" },
    { id: 3, name: "b" },
    { id: 4, name: "c" }
]

// Filter condition function getName(val) {
    return arrTest => arrTest.name === val
}
// If we want to find the first data that meets the conditions, we should use `Array.find`
console.log(arrTest.find(getName("b")))
// { id: 2, name: "b" }

Array.some

Array.some returns a Boolean value indicating whether the condition is met.

const arrTest = [
    { id: 1, name: "a", status: "loading" },
    { id: 2, name: "b", status: "loading" },
    { id: 3, name: "b", status: "success" }
]

// Filter condition function getStatus(val) {
    return arrTest => arrTest.status === val
}

// If we need to find out whether a certain data exists in an array, use Array.some to get the result directly console.log(arrTest.some(getStatus("success")))
// true

Array.filter

Array.filter traverses the entire Array and returns an array (containing all objects that meet the conditions)

const arrTest = [
    { id: 1, name: "a", status: "loading" },
    { id: 2, name: "b", status: "loading" },
    { id: 3, name: "b", status: "success" }
]

// Filter condition function getStatus(val) {
    return arrTest => arrTest.status === val
}

 
// If we need to filter out all the data that meets the conditions in an array, we should use Array.filter
console.log(arrTest.filter(getStatus("loading")))
// [
// { id: 1, name: "a", status: "loading" },
// { id: 2, name: "b", status: "loading" }
// ]

Array.reduce

Array.reduce is an array merging method, which can be used in many scenarios, such as summing, multiplication, counting, deduplication, converting multi-dimensional data to single-dimensional data, summing attributes, etc.
The examples in this section mainly implement Array.reduce to return a new array after conditionally filtering a set of data.

const arrTest = [
    { id: 1, status: "loading" },
    { id: 2, status: "loading" },
    { id: 3, status: "success" }
]

console.log(
    arrTest.reduce((acc, character) => {
        return character.status === "loading"
            ? acc.concat(
                  Object.assign({}, character, { color: "info" })
              )
            : acc
    }, [])
)
// [
// { id: 1, status: "loading", color: "info" },
// { id: 2, status: "loading", color: "info" }
// ]

Unlike the array returned by Array.filter, filter returns a set of objects that meet the conditions in the original array. The above result can also be achieved by combining filter with Array.map. Why is it better to use reduce?

// Array.map and Array.filter combined console.log(
    arrTest
        .filter(character => character.status === "loading")
        .map(character =>
            Object.assign({}, character, { color: "info" })
        )
)
// [
// { id: 1, status: "loading", color: "info" },
// { id: 2, status: "loading", color: "info" }
// ]

Conclusion: When using Array.filter and Array.map at the same time, the entire array is looped twice. The first time is to filter and return a new array, and the second time is to construct a new array through map. Two array methods are used, each with its own callback function, and the array returned by filter is never used again.
Using Array.reduce gives the same result, but with more elegant code.

This is the end of this article about the detailed explanation of the differences between js array find, some, filter, and reduce. For more related js array find, some, filter, and reduce content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the reduce method of arrays in JavaScript
  • Detailed explanation of JS array Reduce() method and advanced techniques
  • js array method reduce classic usage code sharing
  • 25 advanced uses of JS array reduce that you must know
  • Analysis of the principle and usage skills of JS array reduce() method
  • Sharing the magic of Javascript array method reduce
  • Detailed explanation of the function and usage of JS array Reduce method
  • Parsing JavaScript array method reduce
  • Example analysis of the usage of JS array method reduce
  • JavaScript array reduce common instance methods

<<:  Quickly solve the problem that CentOS cannot access the Internet in VMware

>>:  Why does MySQL database index choose to use B+ tree?

Recommend

MySQL 1130 exception, unable to log in remotely solution

Table of contents question: 1. Enable remote logi...

Detailed explanation of js event delegation

1. Each function is an object and occupies memory...

Solution to the problem that elements with negative z-index cannot be clicked

I was working on a pop-up ad recently. Since the d...

Detailed explanation of mysql filtering replication ideas

Table of contents mysql filtered replication Impl...

Docker custom network container interconnection

Table of contents Preface –link Custom Network As...

Detailed implementation plan of Vue front-end exporting Excel files

Table of contents 1. Technology Selection 2. Tech...

Details on how to use class styles in Vue

Table of contents 1. Boolean 2. Expression 3. Mul...

The difference between MySQL database stored procedures and transactions

Transactions ensure the atomicity of multiple SQL...

Detailed explanation of meta tags and usage in html

I won’t waste any more time talking nonsense, let...

Summary of the application of transition components in Vue projects

​Transtion in vue is an animation transition enca...

Detailed explanation of the role of brackets in AngularJS

1. The role of brackets 1.1 Square brackets [ ] W...