简体   繁体   English

如何使用Ramda.js映射对象值并仅采用第一个元素?

[英]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 我想用ramda.js来做

The simplest answer is just map(head, obj) , or if you want a reusable function, map(head) . 最简单的答案就是map(head, obj) ,或者如果你想要一个可重用的函数, map(head)

You can see this in action on the Ramda REPL . 你可以在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) (这个确切的代码不起作用,因为没有定义obj1obj2obj3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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