简体   繁体   中英

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

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

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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM