I am using Redux. In my reducer I'm trying to remove a property from an object like this:
const state = {
a: '1',
b: '2',
c: {
x: '42',
y: '43'
},
}
And I want to have something like this without having to mutate the original state:
const newState = {
a: '1',
b: '2',
c: {
x: '42',
},
}
I tried:
let newState = Object.assign({}, state);
delete newState.c.y
but for some reasons, it deletes the property from both states.
Could help me to do that?
Also, if looking for a functional programming toolkit, look at Ramda.
Just use ES6 object destructuring feature
It's easy with Immutable.js:
description of deleteIn()
You can use
_.omit(object, [paths])
from lodash librarypath can be nested for example:
_.omit(object, ['key1.key2.key3'])
How about this:
It filters the key that should be deleted then builds a new object from the remaining keys and the initial object. The idea is stolen from Tyler McGinnes awesome reactjs program.
JSBin
You may use Immutability helper in order to unset an attribute, in your case: