在 Immutable js Map 中执行嵌套更新

Performing nested update in Immutable js Map

我最近开始研究 Immutable.js,但缺乏有用的示例非常令人沮丧。

我有一个深度嵌套的不可变映射,其结构如下所示。我需要使用 immutable.js 来更新 ID 为“264”的章节的页面数据。

var publisherData = {
"id": 367,
"publisher": {
    "author": {
        "id": 13,
        "books": [
            {
                "id": 13,
                "sections": [
                    {
                        "id": 49,
                        "chapters": [
                            {
                                "id": 262,
                                "pages": null
                            },
                            {
                                "id": 263,
                                "pages": null
                            },
                            {
                                "id": 264,
                                "pages":null
                            },
                            {
                                "id": 265,
                                "pages": {
                                    "id": 159
                                },
                            }
                        ]
                    },
                    {
                        "id": 50,
                        "chapters": [
                            {
                                "id": 266,
                                "pages": null
                            },
                            {
                                "id": 267,
                                "pages": null
                            },
                            {
                                "id": 268,
                                "pages": null
                            },
                            {
                                "id": 269,
                                "pages": null
                            },
                            {
                                "id": 270,
                                "pages": null
                            },
                            {
                                "id": 271,
                                "pages": null
                            },
                            {
                                "id": 272,
                                "pages": null
                            }
                        ]
                    },
                    {
                        "id": 51,
                        "chapters": [
                            {
                                "id": 273,
                                "pages": null
                            }
                        ]
                    },
                    {
                        "id": 52,
                        "chapters": [
                            {
                                "id": 277,
                                "pages": null
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

我设法使用这段代码获得了相关的 moduleIndex 和 sectionIndex -

    immutableStub.getIn(["publisher","author", "books"]).map((module,i) => {
  return module.get('sections').map((section,j) => {
    return section.get('chapters').map((chapter,k) => {
      if(chapter.get('id') == '273'){
        moduleIndex = i;
        sectionIndex = j;
        chapterIndex = k;
      }
    })
  })
})

我该如何继续?我不能使用 'updateIn',因为 keyPath 仅部分延伸 - "publisher"、"author"、"books" - 然后它是一个列表。

请提出解决方案。

你快到了...

immutableStub.getIn([ "publisher", "author", "books" ]).map(( module, i ) => {
    return module.get('sections').map(( section, j ) => {
        return section.get('chapters').map(( chapter, k ) => {
            if ( chapter.get('id') == '273' ) {
                moduleIndex = i;
                sectionIndex = j;
                chapterIndex = k;
                chapterToUpdate = chapter;
            }
        })
    })
})
if ( chapterToUpdate ) {
    // ...make changes to chapterToUpdate...
    immutableStub = immutableStub.setIn([
        "publisher",
        "author",
        "books",
        moduleIndex,
        "sections",
        sectionIndex,
        "chapters",
        chapterIndex
    ], chapterToUpdate);
}