简体   繁体   English

使用 ramda 在嵌套 object 中进行数组过滤

[英]Array filtering in nested object using ramda

Let's assume that we have the following object:假设我们有以下 object:

const sample = {
  foo: {
    tags: [
      'aaa', 'bbb'
    ],
    a: 1,
    b: 10
  },
  bar: {
    tags: [
      'ccc', 'ddd'
    ],
    a: 11,
    b: 100
  }
}

How can one remove a specific tag value from object sample using ramda?如何使用 ramda 从 object sample中删除特定标签值? I have done this我已经这样做了

/// Remove tag named 'aaa'
R.map(v => R.assoc('tags', R.without('aaa', v.tags), v), sample)

which achieves the desired result but how can I eliminate the lamda (and the closure created) inside map?这达到了预期的结果,但我怎样才能消除 map 内的 lamda (和创建的闭包)?

You could use evolve instead of assoc .您可以使用evolve而不是assoc assoc expects a property and plain value to set on provided object, whereas evolves expects a property and function producing the new value (although in a slightly other syntax). assoc期望在提供的 object 上设置一个属性和普通值,而 Evolutions 期望一个属性和evolves产生新值(尽管语法稍有不同)。

R.map(R.evolve({tags: R.without('aaa')}), sample)

You can R.evolve each object, and use R.without to transform the value of tags :您可以R.evolve每个 object,并使用 R.without 来转换tags的值:

 const { map, evolve, without } = R const fn = map(evolve({ tags: without('aaa') })) const sample = {"foo":{"tags":["aaa","bbb"],"a":1,"b":10},"bar":{"tags":["ccc","ddd"],"a":11,"b":100}} const result = fn(sample) console.log(result)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>

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

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