How to split two immutable objects
Is there a way to compare objects in Immutable ?
Example:
let a = Immutable.fromJS({ a: 10 });
let b = Immutable.fromJS({ b: 10 });
let d = Immutable.diff(a, b);
// { a: 10 }
console.log(d.toJS());
This would be helpful when testing to see how different objects are.
It shouldn't show what has been added / removed explicitly. I just want to see the properties that are a
different from b
so that I can make them equal again.
For example, I do this:
console.log(a.toJS());
console.log(b.toJS());
and check the logs to see where a differs from b.
In another thought, it can show both properties added
and removed
in the same release. The main thing is to see where they are not equal.
so that it can also be:
// { a: 10, b: 10 }
console.log(d.toJS());
or perhaps:
// { added: { a: 10 }, removed: { b: 10 } }
// console.log(d.toJS());
source to share
Usually a person can see the difference pretty quickly, so knowing that they are different is quite common: Immutable.is () compares two collections and tells you when they do not contain the same elements.
If that's not enough, use something like the deep-diff npm package :
const diff = require('deep-diff').diff;
var differences = diff(a.toJS(), b.toJS());
copnsole.log('diff', differences);
source to share