简体   繁体   中英

Array filtering in nested object using ramda

Let's assume that we have the following 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? 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?

You could use evolve instead of 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).

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 :

 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>

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