5

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

John Taylor
  • 729
  • 3
  • 11
  • 22

2 Answers2

12

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.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • Yes, that is much better! Simpler and more elegant! I was trying to use head, but not in exactly right way before. – John Taylor Sep 07 '17 at 00:36
3
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)

Sidney
  • 4,495
  • 2
  • 18
  • 30
  • 2
    Note that since `key` is not used, you could simplify this to `R.map(value => value[0], data)`. (Ramda treats Objects as functors that it knows how to map.) But Ramda's `head` is essentially `xs => xs[0]`, so this could also be written as `R.map(R.head, data)`, and we arrive at my solution. – Scott Sauyet Sep 07 '17 at 00:33