简体   繁体   English

Immutablejs - 更新嵌套 Map 和 List 中的值

[英]Immutablejs - Update Values in nested Map and List

I have this immutableJs map:我有这个 immutableJs 地图:

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?关于如何使用 immutableJs 做到这一点的任何想法?

Late comer to the party, but how about this (assuming your data is in a variable called data ) :迟到的人,但是这个怎么样(假设你的数据在一个名为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....在 ES6 中,它可能可以很容易地转换为 immutableJS....

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

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

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