Javascript 如何获得两个不可变映射之间的差异?

Javascript 如何获得两个不可变映射之间的差异?,javascript,frontend,immutability,Javascript,Frontend,Immutability,我有两个不变的映射: const first = immutable.Map({ one: 1, two: 2 }); const two = immutable.Map({ one: 1, two: 2, three: 3 }); 如何获得差异? 我应该得到: { three: 3 } // Map because need merge this data in the feature 在immutable 4.0.0-rc.12中,您可以 const first = Immutable.

我有两个不变的映射:

const first = immutable.Map({ one: 1, two: 2 });
const two = immutable.Map({ one: 1, two: 2, three: 3 });
如何获得差异? 我应该得到:

{ three: 3 } // Map because need merge this data in the feature

在immutable 4.0.0-rc.12中,您可以

const first = Immutable.Map({ one: 1, two: 2 });
const two = Immutable.Map({ one: 1, two: 2, three: 3 });
console.log(two.deleteAll(first.keys()).toJS())
达到类似的效果
deleteAll
在版本3中不是一个东西,所以我想你可以从地图关键点生成集合并与它们相交

const m1 = new Immutable.Map({foo: 2, bar: 42});
const m2 = new Immutable.Map({foo: 2, bar: 42, buz: 44});


const s1 = Immutable.Set(m1.keys());
const s2 = Immutable.Set(m2.keys());

const s3 = s2.subtract(s1);
const tmp = {};
for (let key of s3.keys()) {
  tmp[key] = m2.get(key);
}

const res = new Immutable.Map(tmp);
console.log(res.toJS());

在immutable 4.0.0-rc.12中,您可以

const first = Immutable.Map({ one: 1, two: 2 });
const two = Immutable.Map({ one: 1, two: 2, three: 3 });
console.log(two.deleteAll(first.keys()).toJS())
达到类似的效果
deleteAll
在版本3中不是一个东西,所以我想你可以从地图关键点生成集合并与它们相交

const m1 = new Immutable.Map({foo: 2, bar: 42});
const m2 = new Immutable.Map({foo: 2, bar: 42, buz: 44});


const s1 = Immutable.Set(m1.keys());
const s2 = Immutable.Set(m2.keys());

const s3 = s2.subtract(s1);
const tmp = {};
for (let key of s3.keys()) {
  tmp[key] = m2.get(key);
}

const res = new Immutable.Map(tmp);
console.log(res.toJS());

如果这是一个平面对象,我认为你可以得到键作为列表,那么很容易得到差异,它将返回你只存在于其中一个对象中的键,然后你迭代这个列表生成一个新的对象,得到值如果这是一个平面对象,我认为你可以得到键作为列表,那么很容易得到差异,它将返回仅存在于其中一个对象中的键,然后迭代此列表以生成一个获取值的新对象