Reputation: 4802
I need to turn this:
let arr = [{ id: 1, name: 'rod'} , { id: 2, name: 'hey' }]
into:
mapO = { 1: 'rod', 2: 'hey' }
That's what I tried:
let mapIdName = (o) => {
let ret = {};
ret[o["id"]] = o['name'];
return ret;
}
let mergeIdNames = R.mergeAll(R.map(mapIdName));
mergeIdNames(o)
with the error:
mergeIdNames is not a function
Upvotes: 5
Views: 3653
Reputation: 727
Even using Reduce leads to a fairly terse and readable code:
reduce((acc, curr) =>
R.assoc(curr.id, curr.name, acc), {}, arr)
Or if used as a function:
const f = R.reduce((acc, curr) =>
R.assoc(curr.id, curr.name, acc), {})
Upvotes: 1
Reputation: 24806
You could use R.indexBy
:
> R.map(R.prop('name'), R.indexBy(R.prop('id'), [{id: 1, name: 'rod'}, {id: 2, name: 'hey'}]))
{'1': 'rod', '2': 'hey'}
As a function:
// f :: Array { id :: String, name :: a } -> StrMap a
const f = R.pipe(R.indexBy(R.prop('id')), R.map(R.prop('name')));
f([{id: 1, name: 'rod'}, {id: 2, name: 'hey'}]);
// => {'1': 'rod', '2': 'hey'}
Upvotes: 10