使用Immutable.js的mergeDeep()时丢失属性

时间:2016-01-05 06:32:10

标签: javascript 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对象的不同属性,还是只合并两者中相互的属性?如果不能,我怎样才能得到如上所述的预期合并对象?

1 个答案:

答案 0 :(得分:4)

更改

const current = Immutable.Map({ ... });

const current = Immutable.fromJS({ ... });