简体   繁体   English

immutableJS-updateIn方法无法按预期工作

[英]immutableJS - updateIn method doesn't work as expected

I have a map like this structure in immutable js: 我在不变的js中有一个像这样的结构的地图:

myMap = {
    a: {
        b: {
            c: [
                { d: "something1", ... },
                { d: "something2", ... }
            ],
            ...
        },
        ...
    },
    ...
}

Then I want to update the value of d s. 然后,我想更新d s的值。

I've tried like: 我试过像:

myMap.updateIn(["a", "b", "c"], (each) => {
    each.set("d", "something else");
});

and like: 像:

myMap.updateIn(["a", "b", "c"], (each) => {
    each.map((e) => {
        e.set("d", "something else");
    });
});

But as you know, those does not work. 但是,正如您所知,这些都不起作用。

How can I update the value of d s? 如何更新d s的值?

I've found the reason why my code above doesn't work. 我发现了以上代码无法正常工作的原因。

Firstly, the second code is partially correct, but first one is wrong. 首先,第二个代码部分正确,但是第一个是错误的。

The partially means that the logic was correct, but the grammar of ES6 was not. 部分表示逻辑正确,但ES6的语法不正确。

So, the correct code is: 因此,正确的代码是:

const updatedMyMap = myMap.updateIn(["a", "b", "c"], each => 
    each.map(e => e.set("d", "something else"))
);

In arrow function docs , 箭头功能docs中

(param1, param2, …, paramN) => { statements } (param1,param2,…,paramN)=> {语句}

(param1, param2, …, paramN) => expression (param1,param2,…,paramN)=> 表达式

// equivalent to: (param1, param2, …, paramN) => { return expression; //等于:(param1,param2,…,paramN)=> {返回表达式; } }

this is why my code didn't work. 这就是为什么我的代码不起作用的原因。

I should have returned the result of map() . 我应该返回map()的结果。

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

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