Last active
March 6, 2018 22:35
-
-
Save chriswrightdesign/e55f928158e12ac0e2bbe5dae28a523c to your computer and use it in GitHub Desktop.
array find
This file contains 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
// our getValue function from earlier | |
const getValue = property => obj => obj && obj[property]; | |
// Lets use a pipe function to combine the two functions: | |
const pipe = (func1, func2) => (...args) => func2(func1(...args)); | |
// our list of user objects | |
const users = [{ | |
name: 'sarah', | |
id: '12345', | |
country: 'Australia' | |
}, | |
{ | |
name: 'bob', | |
id: '34325', | |
country 'New Zealand' | |
}, | |
{ | |
name: 'Sam', | |
id: '42524', | |
country: 'Australia' | |
}, | |
{ | |
name: 'Roger', | |
country: 'USA', | |
id: '5135135' | |
}]; | |
const usersNamedBob = pipe(getValue('name'), valueEquals('bob')); | |
users.find(usersNamedBob); | |
// result: | |
{ | |
name: 'bob', | |
id: '34325', | |
country: 'New Zealand' | |
} | |
// If an array returns an array, we can continue to chain our higher order functions: | |
users.filter(usersNamedBob).map(getValue('id')); // result: ['34325']; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment