Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

array/toLookup #5

Open
shannonmoeller opened this issue May 2, 2014 · 4 comments
Open

array/toLookup #5

shannonmoeller opened this issue May 2, 2014 · 4 comments

Comments

@shannonmoeller
Copy link
Member

Converts an array to an object indexed by a particular key.

var records = [
    { id: 'foo', greeting: 'hi' },
    { id: 'bar', greeting: 'howdy' },
    { id: 'baz', greeting: 'sup' }
];
var recordsIndex = records.reduce(toLookup('id'), {});
// recordsIndex === {
//     foo: { id: 'foo', greeting: 'hi' },
//     bar: { id: 'bar', greeting: 'howdy' },
//     baz: { id: 'baz', greeting: 'sup' }
// };
@shannonmoeller
Copy link
Member Author

Multiple args for deep index, a la plucks.

#1 (comment)

@nickstark
Copy link

What if lookups are not unique? Make an array?

Maybe we want a separate groupsBy or something that will deal with this.

@nickstark
Copy link

It also seems like this function would never be used outside of reduce due to its funky arguments signature. Maybe not a big deal, idk.

@shannonmoeller
Copy link
Member Author

We could create the object if it doesn't exist, so that the signature is consistent:

records.reduce(toLookup('id'));

@shannonmoeller shannonmoeller added this to the 1.0.0 milestone Jul 8, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants