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

2024-01-30

我正在尝试合并 2 个不可变的映射,如下所示,使用合并深度() http://facebook.github.io/immutable-js/docs/#/Map/mergeDeepImmutable.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”丢失。

 AssertionError: expected { Object (a, b, ...) } to deeply equal { Object (a, b, ...) }
      + expected - actual

       {
         "a": []
         "b": {}
         "c": {
      +    "c01": {
      +      "key": "c01"
      +    }
           "c02": {
             "key": "c02"
           }
         }

mergeDeep() 可以合并两个不同 Map 对象的不同属性,还是只合并两个对象中共同的属性?如果不能,我怎样才能得到上面预期的合并对象?


change

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

to

const current = Immutable.fromJS({ ... });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Immutable.js 的 mergeDeep() 时丢失属性 的相关文章

随机推荐