Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ramdajs keyBy equivalent of lodash

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.

like image 219
undefined Avatar asked Dec 03 '22 20:12

undefined


1 Answers

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:

  • Github: Ramda Wiki "What Ramda Function Should I Use?"
  • Github: Ramda Issue #931 - "sister to groupBy"
like image 166
Patrick Avatar answered Dec 17 '22 04:12

Patrick