I use the deep-diff Node.js library for comparison of two JSONs. There is also pre-filter condition for omitting few fields for comparison. If my pre-filter condition is multiple attributes, how would I specify that? In the given example, the pre-filter attribute is name
. If I want to add one or more attributes, how would I specify that. Thanks in advance.
var diff = require('deep-diff').diff;
var lhs = {
name: 'my object',
description: 'it\'s an object!',
details: {
it: 'has',
an: 'array',
with: ['a', 'few', 'elements']
}
};
var rhs = {
name: 'updated object',
description: 'it\'s not an object!',
details: {
it: 'has',
an: 'array',
with: ['a', 'few', 'more', 'elements', {
than: 'before'
}]
}
};
var differences = diff(lhs, rhs, function(path, key) {
return key === 'name';
});
console.log(differences);
If I understood you correctly, then it should be as simple as adding an or (||
). For example, do ignore description as well you could write:
var differences = diff(lhs, rhs, function(path, key) {
return key === 'name' || key === 'description';
});
Of course, as you start ignoring lots of items you may want to introduce an array of properties / paths you want to ignore and then simply check in the pre-filter if the current property should be ignored. So a more generic way with an array could be like this:
var ignoredProperties = ['prop1', 'prop2', 'prop3'];
var differences = diff(lhs, rhs, function(path, key) {
return ignoredProperties.indexOf(key) >= 0;
});
With ECMAScript 2016 there are also Set objects which would be better in this case but as this is not widely implemented yet I would use the code above.