在Immutable js-Map中执行嵌套更新

Performing nested update in Immutable js Map

本文关键字:嵌套 更新 执行 Immutable js-Map      更新时间:2023-09-26

我最近开始研究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
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

我设法使用这个代码获得了相关的模块索引和部分索引

    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);
}