How can I set a deeply nested value in Immutable.js?

前端 未结 1 538
生来不讨喜
生来不讨喜 2020-12-29 01:30

When working with plain JavaScript objects it\'s easy to change a deeply nested object property:

people.Thomas.nickname = \"Mr. T\";

But wi

相关标签:
1条回答
  • 2020-12-29 02:09

    Maps in Immutable have a setIn method that makes it easy to set deep values:

    peopleImmutable = peopleImmutable.setIn(["Thomas", "nickname"], "Mr. T");
    

    Or, using split to generate the array:

    peopleImmutable = peopleImmutable.setIn("Thomas.nickname".split("."), "Mr. T");
    
    0 讨论(0)
提交回复
热议问题