使用 ramda 在嵌套对象中进行数组过滤
Array filtering in nested object using ramda
假设我们有以下对象:
const sample = {
foo: {
tags: [
'aaa', 'bbb'
],
a: 1,
b: 10
},
bar: {
tags: [
'ccc', 'ddd'
],
a: 11,
b: 100
}
}
如何使用 ramda 从对象 sample
中删除特定标记值?
我做到了
/// Remove tag named 'aaa'
R.map(v => R.assoc('tags', R.without('aaa', v.tags), v), sample)
这实现了预期的结果,但我如何消除 map 中的 lamda(和创建的闭包)?
您可以使用 evolve
instead of assoc
。 assoc
期望在提供的对象上设置 属性 和纯值,而 evolves
期望 属性 和函数产生新值(尽管语法稍有不同)。
R.map(R.evolve({tags: R.without('aaa')}), sample)
可以R.evolve每个对象,用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>
假设我们有以下对象:
const sample = {
foo: {
tags: [
'aaa', 'bbb'
],
a: 1,
b: 10
},
bar: {
tags: [
'ccc', 'ddd'
],
a: 11,
b: 100
}
}
如何使用 ramda 从对象 sample
中删除特定标记值?
我做到了
/// Remove tag named 'aaa'
R.map(v => R.assoc('tags', R.without('aaa', v.tags), v), sample)
这实现了预期的结果,但我如何消除 map 中的 lamda(和创建的闭包)?
您可以使用 evolve
instead of assoc
。 assoc
期望在提供的对象上设置 属性 和纯值,而 evolves
期望 属性 和函数产生新值(尽管语法稍有不同)。
R.map(R.evolve({tags: R.without('aaa')}), sample)
可以R.evolve每个对象,用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>