Skip to content

Instantly share code, notes, and snippets.

@chriswrightdesign
Last active March 6, 2018 22:35
Show Gist options
  • Save chriswrightdesign/e55f928158e12ac0e2bbe5dae28a523c to your computer and use it in GitHub Desktop.
Save chriswrightdesign/e55f928158e12ac0e2bbe5dae28a523c to your computer and use it in GitHub Desktop.
array find
// 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