Change Value Properties in Object with Ramda Lenses

回眸只為那壹抹淺笑 提交于 2019-12-07 15:02:40

问题


I would like to know how can I change object properties with Ramda Lenses.

Currently, I have a deep state :

buckets[
    blocks[
        messages[
            replies [
                {id: 0, text: 'text 0', value: 'simple value 0'},
                {id: 1, text: 'text 1', value: 'simple value 1'},
                {id: 2, text: 'text 2', value: 'simple value 2'},
                ...
            ]
        ]
    ]
]

I have a basic payload. I would like to get the property and the value, and set the old value by the new value in my state, for example with this payload :

{text: 'new_text'} or {value: 'new_value'}

In my reducer I have this :

case SEQUENCES.UPDATE_REPLY_ON_BLOCK :
// payload => {text: 'new_text'}, or {value: 'new_value'}, or anyway...

let key = Object.keys(payload)[0];
let value = payload[key];

return R.over(
  R.lensPath(["buckets", 0, "blocks", 0, "messages", 0, "replies", 0, key]),
  R.set(value),
  state
);

I tried with Merge :

return R.over(
    R.lensPath(["buckets", 0, "blocks", 0, "messages", 0, "replies", 0),
    R.merge(payload),
    state,
);

But same result : the state is not modified and I have no error.

Maybe SOLVED with mergeDeepLeft :

//payload => {value: 'new_value'}

return R.over(
    R.lensPath(["buckets", 0, "blocks", 0, "messages", 0, "replies", 0),
    R.mergeDeepLeft(payload),
    state,
);

回答1:


You're close. The problem is that R.set and R.over are used for two different tasks. You don't need over here. Instead your outer function should be set and the second parameter the value you want to set the lens to:

const payload = {text: 'new_text'}
const key = Object.keys(payload)[0];
const value = payload[key];

const state = {buckets: [{blocks: [{messages: [{replies: [{id: 0, text: 'text 0', value: 'simple value 0'}, {id: 1, text: 'text 1', value: 'simple value 1'}, {id: 2, text: 'text 2', value: 'simple value 2'}]}]}]}]}

console.log(
    R.set(
        R.lensPath(['buckets', 0, 'blocks', 0, 'messages', 0, 'replies', 0, key]),
        value,
        state,
    )
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

You use over when you want to adjust it based on the value that's already there, passing a function from old value to new value as the second parameter. For instance, R.over(myLens, R.toUpper, obj):

const payload = {text: 'new_text'}
const key = Object.keys(payload)[0];
const value = payload[key];

const state = {buckets: [{blocks: [{messages: [{replies: [{id: 0, text: 'text 0', value: 'simple value 0'}, {id: 1, text: 'text 1', value: 'simple value 1'}, {id: 2, text: 'text 2', value: 'simple value 2'}]}]}]}]}

console.log(
    R.over(
        R.lensPath(['buckets', 0, 'blocks', 0, 'messages', 0, 'replies', 0, key]),
        R.toUpper,
        state,
    )
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>


来源:https://stackoverflow.com/questions/56146263/change-value-properties-in-object-with-ramda-lenses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!