How can I map over object values using Ramda.js and take only first element?

12.9k Views Asked by At

I have input like that:

{
  a: [obj1, obj2, obj3...],
  b: [obj1, obj2, obj3...],
  c: [obj1, obj2, obj3...]
}

I want to have that output (only first object from array for every key)

{
  a: [obj1],
  b: [obj1],
  c: [obj1]
}

I want to do it using ramda.js

2

There are 2 best solutions below

1
On BEST ANSWER

The simplest answer is just map(head, obj), or if you want a reusable function, map(head).

You can see this in action on the Ramda REPL.

1
On
const data = {
  a: [obj1, obj2, obj3],
  b: [obj1, obj2, obj3],
  b: [obj1, obj2, obj3]
}

const updatedData = R.mapObjIndexed( value => ([value[0]]), data)

(This exact code won't work because obj1, obj2, and obj3 aren't defined)