Last active
November 30, 2022 15:33
-
-
Save roden0/86732c92d8291a871421c9d0da961b9d to your computer and use it in GitHub Desktop.
Useful Array reduce use cases. https://itnext.io/useful-reduce-use-cases-91a86ee10bcd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // sequence of functions that you want to be invoked for a specific value | |
| const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x); | |
| // nvokes the functions in reverse order | |
| const compose = (...fns) => x => fns.reverse().reduce((v, f) => f(v), x); | |
| /* | |
| // example: calculating the checkout total | |
| const calculateTotal = pipe( | |
| applyAnySales, | |
| minusStoreCredit, | |
| applyCouponCode, | |
| addTaxes | |
| ) | |
| const cardValue = 250; | |
| const total = calculateTotal(cardValue) | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| When you have a data set that needs to be filtered | |
| by some criteria and you want to manipulate | |
| each remaining item as well | |
| ------------------------------ | |
| the results of all the students that wrote a particular paper, | |
| but you are only interested in the full names of all the students | |
| that had a mark of more than 80. | |
| */ | |
| const studentsData = [ | |
| { | |
| firstName: "Albert", | |
| lastName: "Einstein", | |
| score: 53 | |
| }, | |
| { | |
| firstName: "Charles", | |
| lastName: "Dickens" | |
| score: 84 | |
| }, | |
| { | |
| firstName: "Marilyn", | |
| lastName: "vos Savant", | |
| score: 99 | |
| }, | |
| ]; | |
| const smartestStudents = studentsData.reduce( | |
| (result, student) => { | |
| // do your filtering | |
| if (student.score <= 80) { | |
| return result; | |
| } | |
| // do your mapping | |
| return result.concat(`${student.firstName} ${student.lastName}`); | |
| }, | |
| [] | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // an object as output from an array | |
| const fields = [ | |
| { | |
| type: 'text', | |
| title: 'Title', | |
| name: 'title', | |
| constraints: { | |
| required: { | |
| message: '^Title is required', | |
| allowEmpty: false | |
| } | |
| }, | |
| }, | |
| { | |
| type: 'text', | |
| title: 'Slug', | |
| name: 'slug', | |
| constraints: { | |
| required: { | |
| message: '^Slug is required', | |
| allowEmpty: false | |
| }, | |
| format: { | |
| pattern: '[a-z0-9_-]+', | |
| flags: 'i', | |
| message: '^Can only be a valid slug' | |
| } | |
| }, | |
| }, | |
| // etc. | |
| ]; | |
| const validationRules = fields.reduce( | |
| (rules, field) => Object.assign(rules, { [field.name]: field.constraints }), | |
| {} | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // promises in parallel but in order from a list | |
| const promiseQueue = (promiseFn, list) => | |
| list.reduce( | |
| (queue, item) => queue.then(async result => { | |
| const itemResult = await promiseFn(item); | |
| return result.concat([itemResult]); | |
| }), | |
| Promise.resolve([]) | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // get the total of a complex structure | |
| const cities = [ | |
| { | |
| city: "Chongqing", | |
| population: 30165500 | |
| }, | |
| { | |
| city: "Shanghai", | |
| population: 24183300 | |
| }, | |
| { | |
| city: "Beijing", | |
| population: 21707000 | |
| }, | |
| { | |
| city: "Lagos", | |
| population: 16060303 | |
| }, | |
| ] | |
| const totalPopulation = cities.reduce( | |
| (sum, city) => sum + city.population, | |
| 0 | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment