使用 Ramda.js(和 lens),我希望修改下面的 JavaScript 对象,将 ID 为 "/1/B/i" 的对象中的 "NAME:VERSION1" 更改为 "NAME:VERSION2"。
我想使用 lens,因为我只想更改一个深度嵌套的值,但否则保留整个结构不变。
我不想使用 lensIndex,因为我永远不知道数组的顺序是什么,所以我想通过查找其"id"字段在数组中“查找”对象。
我可以使用 lens 来完成这个操作吗?还是应该用另一种方式?
我想使用 lens,因为我只想更改一个深度嵌套的值,但否则保留整个结构不变。
我不想使用 lensIndex,因为我永远不知道数组的顺序是什么,所以我想通过查找其"id"字段在数组中“查找”对象。
我可以使用 lens 来完成这个操作吗?还是应该用另一种方式?
{
"id": "/1",
"groups": [
{
"id": "/1/A",
"apps": [
{
"id": "/1/A/i",
"more nested data skipped to simplify the example": {}
}
]
},
{
"id": "/1/B",
"apps": [
{ "id": "/1/B/n", "container": {} },
{
"id": "/1/B/i",
"container": {
"docker": {
"image": "NAME:VERSION1",
"otherStuff": {}
}
}
}
]
}
]
}
`function lensMatching(pred) { return R.lens( R.find(pred), (newVal, array, other) => { const index = R.findIndex(pred, array); return R.update(index, newVal, array); } ) }`
对我来说,这似乎更容易与镜头文档相关联。但是,我有什么遗漏吗? - Greg Edwardsfind
中,一次在findIndex
中),但如果数组比较小,这不应该是问题。 - Scott Christopherconst imageLens = R.lensPath(['groups', {id: '/1/B'}, 'apps', {id: '/1/B/i'}, 'container', 'docker', 'image'])
- aweibelllensPath(['groups', 1, 'apps', 1, 'container', 'docker', 'image'])
? - aweibellR.lensIndex
这样的函数属于类似的范畴,尽管它们的实用性被认为是超过了非完整性的潜在风险。如果您对于这个函数有着相似的感觉,那么我建议您在 ramda/ramda github 存储库上提出问题或 PR,以向更广泛的 Ramda 社区提出这个想法。 - Scott Christopher