简体   繁体   中英

How to get difference between two immutable Map?

I have two immutable map:

const first = immutable.Map({ one: 1, two: 2 });
const two = immutable.Map({ one: 1, two: 2, three: 3 });

How to get difference ? I should get:

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

In immutable 4.0.0-rc.12 you can do

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())

to achieve similar result deleteAll wasn't a thing in version 3, so I guess you can produce sets from maps keys and intersect them

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM