Search code examples
javascriptimmutable.js

unable to update the Set property


var npm = require("npm")
var immutable = require("immutable");
var test = immutable.fromJS

const a = test({name:true,b:[]})
console.log(a);
a.set('name',false);

console.log("------------");
console.log(a.get('name')) // gives still value true.

what is the expected value of the last console ? I thought it would be true .Can Someone help me where I went wrong


Solution

  • a isn't reassigned. a.set returns a new object.

    const a = Immutable.fromJS({name:true,b:[]})
    console.log(a);
    const newA = a.set('name',false);
    
    console.log("------------");
    console.log(a.get('name'));
    console.log(newA.get('name'));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>