So I have two JSON objects and I am trying to find difference between them using underscore js. However for some reason its returning me the whole object instead of just returning the difference. My goal here is to get the pattern back as its the only thing different.
var a = {
"name":"donor",
"label":"Donor Data File (donor)",
"pattern":"^donor(\\.[a-zA-Z0-9]+)?\\.txt(?:\\.gz|\\.bz2)?$"
};
var b = {
"name":"donor",
"label":"Donor Data File (donor)",
"pattern":"^donor(\\.[a-zA-Z0-9]+)?\\.txt(?:\\.gz)?$"
};
console.log(_.difference(a,b));
Am I not understanding the use case of _.difference
properly? Heres a JSFiddle in case needed.
So I have two JSON objects and I am trying to find difference between them using underscore js. However for some reason its returning me the whole object instead of just returning the difference. My goal here is to get the pattern back as its the only thing different.
var a = {
"name":"donor",
"label":"Donor Data File (donor)",
"pattern":"^donor(\\.[a-zA-Z0-9]+)?\\.txt(?:\\.gz|\\.bz2)?$"
};
var b = {
"name":"donor",
"label":"Donor Data File (donor)",
"pattern":"^donor(\\.[a-zA-Z0-9]+)?\\.txt(?:\\.gz)?$"
};
console.log(_.difference(a,b));
Am I not understanding the use case of _.difference
properly? Heres a JSFiddle in case needed.
- 1 There's no such thing as a "JSON Object" – Andreas Commented Jul 29, 2016 at 18:53
- Isn't difference for an array? – epascarello Commented Jul 29, 2016 at 18:55
-
1
_.difference
is for arrays; it sounds more like you want a full-on object diffing solution. E.g., github./flitbit/diff (Well, that's a bad example, but the libraries you seek exist.) – Dave Newton Commented Jul 29, 2016 at 18:56 - @DaveNewton Thanks. This is what I was looking for. – 0_0 Commented Jul 29, 2016 at 19:01
3 Answers
Reset to default 5/**
* Deep diff between two object, using underscore
* @param {Object} object Object pared
* @param {Object} base Object to pare with
* @return {Object} Return a new object who represent the diff
*/
const difference = (object, base) => {
const changes = (object, base) => (
_.pick(
_.mapObject(object, (value, key) => (
(!_.isEqual(value, base[key])) ?
((_.isObject(value) && _.isObject(base[key])) ? changes(value, base[key]) : value) :
null
)),
(value) => (value !== null)
)
);
return changes(object, base);
}
_.difference is meant to pare arrays, you're paring objects. See this answer: using underscore's “difference” method on arrays of objects
Underscore has method isMatch
,but no method that will return difference for objects, that takes 2 as parameter Objects
and match their properties
var stooge = {name: 'moe', age: 32};
_.isMatch(stooge, {age: 32});
you can create your own implementation
function getDiffProperties(object1,object2){
var difference = [];
for(key in object1){
if(object1[key] != object2[key]){
difference.push(key)
}
}
for(key in object2){
if(object1[key] != object2[key]){
difference.push(key)
}
}
return difference
}
console.log(getDiffProperties({name: 'moe', age: 32},{age: 32}))
<script src="https://cdnjs.cloudflare./ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>