jojo jojo - 4 months ago 56
Node.js Question

Pre-filter condition - deep-diff Node.js

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

Answer

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.