简体   繁体   English

通过使用Ramda将值中的字段附加到键中来修改对象的键

[英]Modify Key of the object by appending a field from Value into Key using Ramda

I am trying to apply Ramda on the following problem:- 我正在尝试将Ramda应用于以下问题:-

{
   "alpha": {
      "reg": "alpha1",
      "reg2": "alpha2"
   },
   "beta": {
      "reg": "beta1",
      "reg2": "beta2"  
   }
}

Output 产量

{
   "v:alpha|reg:alpha1": {
      "reg": "alpha1",
      "reg2": "alpha2"
   },
   "v:beta|reg:beta1": {
      "reg": "beta1",
      "reg2": "beta2"  
   }
}

Basically, the output will be the object modifying the key by combining the key and a field from the value into the key and forming the new key. 基本上,输出将是通过将键和值中的字段组合到键中并形成新键来修改键的对象。

For eg if the key="alpha", value is an object with key reg="alpha1". 例如,如果key =“ alpha”,则值为带有键reg =“ alpha1”的对象。 so the key should be modified to v:alpha|reg:alpha1 . 因此应将密钥修改为v:alpha|reg:alpha1 v being unique string appending at the start of every key, and then appending reg:alpha1 to the key. v是唯一的字符串,附加在每个键的开头,然后将reg:alpha1附加到键。

Thanks 谢谢

I think what people are saying in the comments is mostly right. 我认为人们在评论中所说的大部分都是正确的。 (Disclaimer: I'm one of the authors of Ramda.) (免责声明:我是Ramda的作者之一。)

Let's imagine Ramda has a renameBy function: 让我们想象一下renameBy有一个renameBy函数:

const renameBy = curry((fn, obj) => pipe(
  toPairs,
  map(pair => [apply(fn, pair), pair[1]]),
  fromPairs
)(obj))

Ramda doesn't include this now, but there is a slightly less powerful version in Ramda's Cookbook . Ramda现在不包括此功能,但Ramda的Cookbook中的 功能稍差一些 It's certainly conceivable that it will one day be included. 当然可以考虑将其中的某天包括在内。 But failing that, you could include such a function in your own codebase. 否则,您可以在自己的代码库中包含这样的功能。 (If we really tried, we could probably make that points-free, but as I'm trying to explain, that is probably unnecessary.) (如果我们真的尝试过,我们可能可以使该点没有问题,但是正如我试图解释的那样,这可能是不必要的。)

How much does this gain you? 这能使您受益多少?

You could then write a transformation function like 然后,您可以编写一个转换函数,例如

const transform = renameBy((k, v) => `v:${k}|reg:${v.reg}`);

Granted, that is now simpler than the one in zerkms's comment 当然,这现在比zerkms的评论要简单

But the fundamentally complex bit of that version is retained here: the function that performs string interpolation on your key and value to yield a new key. 但是这里保留了该版本的基本复杂部分:对您的键和值执行字符串插值以产生新键的函数。 Any attempt to make that points-free is likely to be substantially uglier than this version. 任何使该点无分数的尝试都可能比该版本难看得多。

Perhaps this is worth it to you. 也许这对您来说值得。 It does separate concerns between the action of renaming keys and your particular key-generation scheme. 它在重命名密钥的操作和您特定的密钥生成方案之间确实分开了关注点。 And it reduces visual complexity... but only if you move that renameBy out of sight into some utilities library. 而且它降低了视觉上的复杂性……但是仅当您将重renameBy移到某些实用程序库中renameBy可见。 If you're going to have multiple uses of renameBy , or if you simply prefer building up a more complete utility library in order to keep your other code more focused, then this might be a reasonable approach. 如果您将要使用renameBy多种用法,或者只是为了建立更多完整的实用程序库以使您的其他代码更加集中renameBy ,那么这可能是一种合理的方法。

But it doesn't reduce the essential complexity of the code. 但这并没有降低代码的本质复杂性。

I do look to points-free solutions when it's reasonable. 我会在合理的情况下寻求无积分的解决方案。 But they are just another tool in my toolbox. 但是它们只是我工具箱中的另一个工具。 They are only worth it if they make the code more readable and maintainable. 只有使代码更具可读性和可维护性,它们才值得。 If they add complexity, I would suggest you don't bother. 如果它们增加了复杂性,我建议您不要打扰。

You can see this version in the Ramda REPL . 您可以在Ramda REPL中看到此版本。

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

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