Ramda js: lens for deeply nested objects with nested arrays of objects

后端 未结 2 1438
慢半拍i
慢半拍i 2021-02-01 06:11

Using Ramda.js (and lenses), I want to modify the JavaScript object below to change \"NAME:VERSION1\" to \"NAME:VERSION2\" for the object that has ID= \"/1/B/i\".

I want

2条回答
  •  臣服心动
    2021-02-01 06:33

    Here's one solution:

    const updateDockerImageName =
    R.over(R.lensProp('groups'),
           R.map(R.over(R.lensProp('apps'),
                        R.map(R.when(R.propEq('id', '/1/B/i'),
                                     R.over(R.lensPath(['container', 'docker', 'image']),
                                            R.replace(/^NAME:VERSION1$/, 'NAME:VERSION2')))))));
    

    This could be decomposed into smaller functions, of course. :)

提交回复
热议问题