I have an array that I want to transform into an object. For example:
const arr = [{id: 1, key: ''}, {id: 2, key: ''}];
I want the result to be:
const object = { 1: {id: 1, key: ''}, 2: { id: 2, key: ''}}
With lodash
I can use the keyBy
function, but I am working with ramda and did not find this functionality there.
In case anyone still finds this via search, the correct answer is indexBy
, added in mid-2016.
const list = [
{ id: "xyz", title: "A" },
{ id: "abc", title: "B" },
];
R.indexBy(R.prop("id"), list);
//=> {abc: {id: 'abc', title: 'B'}, xyz: {id: 'xyz', title: 'A'}}
See Also:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With