Giter Site home page Giter Site logo

Comments (2)

NareshNaredla avatar NareshNaredla commented on May 20, 2024

and tried like this also : map.set(['b','d'], function(value) { return 1000; })

from immutable-js.

leebyron avatar leebyron commented on May 20, 2024

Since that value is a mutable object, not an immutable map, the immutable API does not apply to it (see Object's API). If your update backs up one level, then it points to the object instead of the key within the object:

var map1 = Immutable.Map({a:1, b:{d:2}, c:3});
var map2 = map1.update('b', function (value) { value.d = 1000; return value; })

However, note that this has mutated a mutable value, which your original map still points to, so in this case map2 === map1.

You could also return a copy of this object:

var map1 = Immutable.Map({a:1, b:{d:2}, c:3});
var map2 = map1.update('b', function (value) { 
  var newValue = {};
  for (var k in value) {
    if (value.hasOwnProperty(k)) {
      newValue[k] = value[k];
    }
  }
  newValue.d = 1000; 
  return newValue; 
})

And now map2 !== map1 and map1's mutable object has not been changed. However this highlights one of the use cases of Immutable Map. If you desired the second behavior, then using an immutable Map instead of a mutable Object as a nested value would provide an API much easier to work with:

var map1 = Immutable.Map({a:1, b:Immutable.Map({d:2}), c:3});
var map2 = map1.update('b', function (value) { return value.set('d', 1000); })

from immutable-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.