I have an array:
var a = [
{id: 1, val: 'a'},
{id: 2, val: 'b'},
{id: 3, val: 'c'},
{id: 4, val: 'd'},
]
And I want to get transform it to:
var b = {
1: 'a',
2: 'b',
3: 'c',
4: 'd',
}
Actually I'm using pure js:
var b = a.reduce(
(ac, pr) => ({
...ac,
[pr.id]: pr.val,
}),
{}
);
But maybe Ramda.js have something special for that purpose?
idis coerced toStringduring the transformation. Type coercion can lead to subtle bugs. Instead of usingObject's as dictionaries, you should use a proper dictionary:Map, where keys can actually beNumbers.Maprather than a plain object, you can instantiate it usingvar b = new Map(a.map(({id, val}) => [id, val]))