Provide Map.setIn()
See original GitHub issueWhat 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:
- Created 9 years ago
- Comments:16 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Nope, after this diff landed, you can now write:
oldMap.setIn([‘test1’, ‘test2’, ‘d’], 4);
@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?