本文介绍了使用Immutable.js的mergeDeep()时丢失属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 mergeDeep合并以下2个不可变地图() of Immutable.js
I'm trying to merge 2 immutable maps as below, using mergeDeep() of Immutable.js
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'.
However, the property 'c01' is missing after merging.
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对象的不同属性,还是只合并两个属性是互斥的?如果不能,如何获得上述期望的合并对象?
Can mergeDeep() do the merging for different properties from 2 different Map objects or only merge properties which are mutual in both? If it can't, how can I get the expected merged object as above?
推荐答案
更改
const current = Immutable.Map({ ... });
到
const current = Immutable.fromJS({ ... });
这篇关于使用Immutable.js的mergeDeep()时丢失属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!