Easily "set", "merge", "remove" nested objects and arrays in an immutable manner.
"immutable-data" lets you specify the part of the data you want to change, and then set the value, then it can change the part that you want to change, at the same time, the rest data that you do not want to change will not be affected.
-
data: original data you want to change
-
map: the path map
{ 'a.b.0': 1 // or 'a["b"][0]': 1 }
eg:
var set = require("immutable-data").set;
var data = {
a: {
b: 2
},
c: [{
d: 2
}]
};
set(data, {
'a.b': 1,
'c.0.d': 1
})
//return
{
a: {
b: 1
},
c: [{
d: 1
}]
}
-
data: original data you want to change
-
object: deep merge object
eg:
var merge = require("immutable-data").merge;
var data = {
a: {
b: 2
},
c: [{
d: 2
}]
};
merge(data, {
a: {
b: 1
},
c: {
"0": {
d: 1
}
}
})
//return
{
a: {
b: 1
},
c: [{
d: 1
}]
}
Tip: If the type of a value is an array, it will be assigned a direct value.
merge({list:[1,2]}, {list:[0]})
//return
{list:[0]}
merge({list:[1,2]}, {list:{"0":0}})
//return
{list:[0,2]}
-
data: original data you want to change
-
path: String or Array
"a.b" ["a.b","a.c"]
eg:
var remove = require("immutable-data").remove;
var data = {
a: {
b: 2
},
c: [{
d: 2
}]
};
remove(data, [
'a.b',
'c.0'
])
// return
{
a: {},
c: []
}
$ npm install
$ npm run dev
$ npm test
$ npm run build
MIT License