question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide Map.setIn()

See original GitHub issue

What is the preferred way to set a nested value? I see that there is updateIn(), which kind of does what I want, but it has two problems:

  • It doesn’t set new keys, it can only update already existing keys.
  • It uses an extraneous function for updating. The value I want to set is independent from the original value (undefined), so I don’t need any function to determine the new value.

How would you add a new key ‘d’ with a single line? What’s the API for this?

var mutableObject = {
  test: { test2: { a:1, b:2, c:3 } }
};
var immutableObject = Immutable.fromJS(mutableObject);

// desired mutation:
// {
//   test: { test2: { a:1, b:2, c:3, d:4 } }
// );

// with mutable objects it's very simple:
mutableObject.test.test2.d = 4;

// with immutable objects it doesn't seem to be possible:
immutableObject = immutableObject.setIn(['test', 'test2', 'd'], 4); // TypeError

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
leebyroncommented, Jul 1, 2016

Nope, after this diff landed, you can now write:

oldMap.setIn([‘test1’, ‘test2’, ‘d’], 4);

0reactions
evenstensbergcommented, Jul 1, 2016

@leebyron do we still need to use newMap = oldMap.updateIn(['test1', 'test2'], x => x.set('d', 4)); to add a prop to the object or how do you do this in a “cleaner” way?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Provide Map.setIn() · Issue #28 · immutable-js ... - GitHub
I see that there is updateIn() , which kind of does what I want, but it has two problems: It doesn't set new...
Read more >
setIn - Immutable.js
setIn(). Returns a copy of the collection with the value at the key path set to the provided value. setIn<C>(collection: C, keyPath: Iterable<unknown>, ......
Read more >
immutable.setIn JavaScript and Node.js code examples
Most used immutable functions. Map. Creates a new Immutable Map. set. Returns a new Map also containing the new key, value pair. If ......
Read more >
How to use setIn with immutable.js List - Stack Overflow
Turning simple object to Map const stateMapped = fromJS(state);. Getting Data from nested structure console.log(stateMapped.
Read more >
Map.prototype.set() - JavaScript - MDN Web Docs - Mozilla
The set() method adds or updates an entry in a Map object with a specified key and a value. Try it.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found