简体   繁体   中英

Immutablejs - Update Values in nested Map and List

I have this immutableJs map:

event: {
    days: [
        {
            date: 1,
            sessions: [
                {
                    id: 1,
                    name: 2,
                    startTime: 1,
                    endTime: 1,
                    description: 1,
                    detailsLink: 1,
                    details: {visible: true}
                },
                {
                    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: true}
                }
            ]
        },
        {
            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: true}
                }
            ]
        }
    ]
}

I want to update the ones that are visible: true to visible:false. I tried a lot of different ways with filter, seq, etc. But no luck.

Any ideas of how I could do this with immutableJs?

Late comer to the party, but how about this (assuming your data is in a variable called data ) :

data.setIn(["event", "days"],
    data.getIn(["event", "days"]).map(day => 
        day.set("sessions", day.get("sessions").map(session => 
            session.setIn(['details', 'visible'], false)))
    )
)

In ES6, which probably can be easily translated into immutableJS....

const notVisibleEvents = {
    ...events,
    days: events.days.map(d => {
        return {
            ...d,
            sessions: d.sessions.map(s => {
                return {
                    ...s,
                    details: {
                        ...s.details,
                        visible: false
                    }
                };
            })
        };
    })
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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