使用 Immutable.js 的 mergeDeep() 时丢失属性
Losing properties when using mergeDeep() of Immutable.js
我正在尝试使用 Immutable.js
的 mergeDeep() 合并 2 个不可变映射,如下所示
import { expect } from 'chai';
import Immutable from 'immutable';
describe.only('Test', () => {
it('should return correct merged objects', () => {
const current = Immutable.Map({
a: [],
b: {},
c: {
'c01': {
key: 'c01'
}
}
});
const next = {
a: [],
b: {},
c: {
'c01': {
key: 'c01'
},
'c02': {
key: 'c02'
}
}
};
const newObj = Immutable.Map({
a: [],
b: {},
c: {
'c02': {
key: 'c02'
}
}
});
expect(current.mergeDeep(newObj).toJSON()).to.deep.equal(next);
});
});
但是,属性 'c01' 合并后丢失了。
AssertionError: expected { Object (a, b, ...) } to deeply equal { Object (a, b, ...) }
+ expected - actual
{
"a": []
"b": {}
"c": {
+ "c01": {
+ "key": "c01"
+ }
"c02": {
"key": "c02"
}
}
mergeDeep() 是否可以对来自 2 个不同 Map 对象的不同属性进行合并,或者只合并两者共有的属性?如果不能,我怎样才能像上面那样得到预期的合并对象?
改变
const current = Immutable.Map({ ... });
至
const current = Immutable.fromJS({ ... });
我正在尝试使用 Immutable.js
的 mergeDeep() 合并 2 个不可变映射,如下所示import { expect } from 'chai';
import Immutable from 'immutable';
describe.only('Test', () => {
it('should return correct merged objects', () => {
const current = Immutable.Map({
a: [],
b: {},
c: {
'c01': {
key: 'c01'
}
}
});
const next = {
a: [],
b: {},
c: {
'c01': {
key: 'c01'
},
'c02': {
key: 'c02'
}
}
};
const newObj = Immutable.Map({
a: [],
b: {},
c: {
'c02': {
key: 'c02'
}
}
});
expect(current.mergeDeep(newObj).toJSON()).to.deep.equal(next);
});
});
但是,属性 'c01' 合并后丢失了。
AssertionError: expected { Object (a, b, ...) } to deeply equal { Object (a, b, ...) }
+ expected - actual
{
"a": []
"b": {}
"c": {
+ "c01": {
+ "key": "c01"
+ }
"c02": {
"key": "c02"
}
}
mergeDeep() 是否可以对来自 2 个不同 Map 对象的不同属性进行合并,或者只合并两者共有的属性?如果不能,我怎样才能像上面那样得到预期的合并对象?
改变
const current = Immutable.Map({ ... });
至
const current = Immutable.fromJS({ ... });