简体   繁体   English

如何使用Ramda将对象映射转换为数组?

[英]How to transform object map to array with Ramda?

I'd like to transfrom the following object: 我想转换以下对象:

{
  'id-1': { prop: 'val1' },
  'id-2': { prop: 'val2' },
}

To array: 数组:

[
  { id: 'id-1', prop: 'val1' },
  { id: 'id-2', prop: 'val2' },
]

What I have done so far (it works): 到目前为止我所做的(有效):

R.pipe(
  R.toPairs,
  R.map(([id, props]) => ({
    id,
    ...props,
  }))
)

I'd like to solve it using Ramda only - if possible. 我只想使用Ramda解决它-如果可能的话。

I'd suggest that solving it "using Ramda only" is a bad design goal, unless this is an exercise in learning Ramda. 我建议“仅使用Ramda”解决它是一个糟糕的设计目标,除非这是学习Ramda的练习。 I'm one of the founders of Ramda and a big fan, but Ramda is only a toolkit meant to simplify your code, to make it easier to work in a certain paradigm. 我是Ramda的创建者之一,也是一个忠实的拥护者,但是Ramda只是一个工具包,旨在简化您的代码,从而使您可以更轻松地在某些范式中工作。

That said, we could certainly write a point-free version of this using Ramda. 也就是说,我们当然可以使用Ramda编写此版本的无点版本。 The first thing that comes to my mind would be this * : 我想到的第一件事就是这个*

 const transform = pipe( toPairs, map(apply(useWith(merge, [objOf('id'), identity]))) ) const data = {'id-1': { prop: 'val1' }, 'id-2': { prop: 'val2'}} console.log(transform(data)) 
 <script src="https://bundle.run/ramda@0.26.1"></script><script> const {pipe, toPairs, map, apply, useWith, merge, objOf, identity} = ramda </script> 

But this is less readable than your original, not more. 但是,这是不是你原来的,而不是更多可读性。

This code: 这段代码:

const transform = pipe(
  toPairs,
  map(([id, props]) => ({...props, id}))
)

is crystal-clear, whereas that Ramda version requires one to understand Ramda-specific useWith and objOf and slightly obscure apply -- I would hope that map , merge , and identity are clear. 是非常清晰的,而useWith版本需要一个才能理解特定于useWithobjOf并且apply objOf有些晦涩-我希望mapmergeidentity是清晰的。

In fact, this code is simple enough that I might well write it as a one-liner, in which case, I switch to compose over pipe : 实际上,这段代码非常简单,我很可能将它编写为单行代码,在这种情况下,我切换到通过pipe compose

const transform = compose(map(([id, props]) => ({...props, id})), toPairs)

But I probably wouldn't do so, as I find that multi-line pipe version easier to read. 但是我可能不会这样做,因为我发现多行pipe版本更易于阅读。

Finally note that we can do this in a fairly readable way without any Ramda tools at all: 最后请注意,我们可以完全不用任何Ramda工具就能以一种易于阅读的方式执行此操作:

const transform = (data) => 
  Object.entries(data).map(
    ([id, props]) => ({...props, id})
  )

If I was already using Ramda in my code-base, I would prefer the pipe version above to this; 如果我已经在代码库中使用Ramda,则我更希望使用上面的pipe版本。 I think it's somewhat easier to read. 我认为它更容易阅读。 But would never introduce Ramda into a project only for that fairly minor difference. 但是绝不会仅出于那微小的差异就将Ramda引入项目中。

I worry that people make a fetish over point-free code. 我担心人们会迷恋无点代码。 It's a tool. 这是一个工具。 Use it when it makes your code more understandable. 当它使您的代码更易于理解时使用它。 Skip it when it makes your code more obscure. 当它使您的代码更加晦涩时,请跳过它。 Here I think you're starting from quite readable code; 在这里,我认为您是从易于阅读的代码开始的。 it's difficult to improve on it. 很难对此进行改进。


* Note that identity here is not strictly necessary; * 请注意 ,此处的identity并非严格必要; you can skip it with no harm. 您可以无害地跳过它。 The function generated by useWith without that identity will incorrectly report an arity of 1, but since the function is immediately wrapped with apply and then further placed in the context of receiving the a two-element array from toPairs , there is nothing which depends upon that arity. useWith生成的没有该identity的函数将错误地报告useWith为1,但是由于该函数立即被apply包裹,然后进一步放置在从toPairs接收一个包含两个元素的数组的toPairs ,因此没有任何依赖于那个的阿里 But I find it a good habit to include it regardless. 但是我发现无论如何都包含它是一个好习惯。

what about this? 那这个呢? probably less verbose! 可能不太详细!

 const toArray = R.pipe( R.toPairs, R.map( R.apply(R.assoc('id')), ), ); const data = { 'id-1': { prop: 'val1' }, 'id-2': { prop: 'val2' }, }; console.log('result', toArray(data)); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script> 

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

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