简体   繁体   English

Immutablejs-使用immutablejs更新深层嵌套树

[英]Immutablejs - Updating deep nested tree with immutablejs

Hello I am trying to update this ImmutableJs tree but having some problems maybe someone can help me out. 您好,我正在尝试更新此ImmutableJs树,但遇到一些问题,也许有人可以帮助我。

Here is the code: 这是代码:

let state = Immutable.Map();
var state1 = state.merge({
    event: {
        days: [
            {
                date: 1,
                sessions: [
                    {
                        id: 1,
                        name: 2,
                        startTime: 1,
                        endTime: 1,
                        description: 1,
                        detailsLink: 1,
                        details: {visible: false}
                    },
                    {
                        id: 2,
                        name: 2,
                        startTime: 2,
                        endTime: 2,
                        description: 2,
                        detailsLink: 2,
                        details: {visible: false}
                    },
                    {
                        id: 3,
                        name: 3,
                        startTime: 3,
                        endTime: 3,
                        description: 3,
                        detailsLink: 3,
                        details: {visible: false}
                    }
                ]
            },
            {
                date: 2,
                sessions: [
                    {
                        id: 1,
                        name: 2,
                        startTime: 1,
                        endTime: 1,
                        description: 1,
                        detailsLink: 1,
                        details: {visible: false}
                    },
                    {
                        id: 2,
                        name: 2,
                        startTime: 2,
                        endTime: 2,
                        description: 2,
                        detailsLink: 2,
                        details: {visible: false}
                    },
                    {
                        id: 3,
                        name: 3,
                        startTime: 3,
                        endTime: 3,
                        description: 3,
                        detailsLink: 3,
                        details: {visible: false}
                    }
                ]
            }
        ]
    }
});

const state2 = state1.setIn(['event','days'], state1.getIn(['event','days']).map(day => {
    return day.get('sessions').map(session => {
        let isVisible = session.get('details').toJS();
        if(!isVisible.visible) {
            return session.setIn(['details','visible'],true);
        }
    })
}))

console.log(state1.toJS());
console.log(state2.toJS());

I am able to update the collection. 我可以更新收藏集。 The problem is that the two trees are not the same anymore. 问题在于两棵树不再相同。 In the first one the days key is an object and in the second one the days key is an array. 在第一个中,days键是一个对象,在第二个中,days键是一个数组。

I know the problem is with the days.get('session') that returns a list and not a map. 我知道问题在于使用days.get('session')返回列表而不是地图。 But not sure how to make it work. 但不确定如何使其工作。

Here is a jsbin of the code. 这是代码的jsbin。

https://jsbin.com/mejaxelobe/1/edit?html,js,output https://jsbin.com/mejaxelobe/1/edit?html,js,output

Thanks 谢谢

Sounds like you are replacing days with sessions which is probably not what you want to do. 听起来您好像要用sessions代替days ,这可能不是您想要的。

If your goal is to set visible true in all the sessions, you could do something like this: 如果您的目标是在所有会话中将visible设置为true,则可以执行以下操作:

const state2 = state1.updateIn(
  ['event','days'],
  days => days.map(
    day => day.update('sessions',
      sessions => sessions.map(
        session => session.updateIn(['details', 'visible'], true)
      )
    )
  )
);

One solution might be when you return, wrap it with "Immutable.Map" ? 一种解决方法是,当您返回时,将其包装为“ Immutable.Map”? For example, the following ?? 例如,以下??

const state2 = state1.setIn(['event','days'], state1.getIn(['event','days']).map(day => {
    return Immutable.Map(day.get('sessions').map(session => {
        let isVisible = session.get('details').toJS();
        if(!isVisible.visible) {
            return session.setIn(['details','visible'],true);
        }
    }))
}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM