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. 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. 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:
|
<<: Quickly solve the problem that CentOS cannot access the Internet in VMware
>>: Why does MySQL database index choose to use B+ tree?
Table of contents question: 1. Enable remote logi...
1. Each function is an object and occupies memory...
I was working on a pop-up ad recently. Since the d...
Table of contents mysql filtered replication Impl...
Table of contents Preface –link Custom Network As...
In many apps and websites, when we log in or regi...
Table of contents 1. Technology Selection 2. Tech...
Database SQL optimization is a common problem. Wh...
Table of contents 1. Boolean 2. Expression 3. Mul...
The crontab command is used by Unix and Linux to ...
Transactions ensure the atomicity of multiple SQL...
I won’t waste any more time talking nonsense, let...
Transtion in vue is an animation transition enca...
1. The role of brackets 1.1 Square brackets [ ] W...
css3 background image related Compatibility: IE9+...