Reputation: 1143
I am trying to use isMatch function of lodash to match below two object but it always giving me true as for i see the length of perm object is different and is not matching either day2 is missing from param array below is the way i tried.
var object = {
param: ['day1', 'day2', 'day3'],
param2: ['day2', 'day3', 'day1', 'day0'],
param3: ['day1']
};
var object2 = {
param: ['day3', 'day1'],
param3: ['day1'],
param2: ['day3', 'day2', 'day0', 'day1']
};
var matched = _.isMatch(object, object2);
console.log(matched)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
The array will be in ordered way if it contains all the value and it matches all the key then all it should return true else false.
Upvotes: 0
Views: 3462
Reputation: 71
_.isEqual(value, other)
It Performs a deep comparison between two values to determine if they are equivalent. It supports comparing arrays, array buffers, booleans, date objects, error objects, maps, numbers, Object objects, regexes, sets, strings, symbols, and typed arrays. Object objects are compared by their own, not inherited, enumerable properties. Functions and DOM nodes are compared by strict equality, i.e. ===
var object = {
param: ['day1', 'day2', 'day3'],
param2: ['day2', 'day3', 'day1', 'day0'],
param3: ['day1']
};
var other = {
param: ['day1', 'day2', 'day3'],
param2: ['day2', 'day3', 'day1', 'day0'],
param3: ['day1']
};
_.isEqual(object, other);
// returns true
It will return true.
var object = {
param: ['day1', 'day2', 'day3'],
param2: ['day2', 'day3', 'day1', 'day0'],
param3: ['day1']
};
var other = {
param: ['day3', 'day1'],
param3: ['day1'],
param2: ['day3', 'day2', 'day0', 'day1']
};
_.isEqual(object, other);
// returns false
It will return false
Upvotes: 0
Reputation: 350137
You could use this plain JavaScript function:
function match(a, b) {
return Object.keys(a).every(key => {
const set = new Set(a[key]);
return key in b && b[key].length === a[key].length
&& b[key].every(val => set.has(val));
});
}
var object = { param: ['day1', 'day2', 'day3'], param2: ['day2', 'day3', 'day1', 'day0'], param3: ['day1'] };
var object2 = { param: ['day3', 'day1'], param3: ['day1'], param2: ['day3', 'day2', 'day0', 'day1'] };
console.log(match(object, object2));
With lodash, you could use difference
on each key/value pair:
function match(a, b) {
return _.every(a, (value, key) => !_.difference(value, b[key]).length);
}
var object = { param: ['day1', 'day2', 'day3'], param2: ['day2', 'day3', 'day1', 'day0'], param3: ['day1'] };
var object2 = { param: ['day3', 'day1'], param3: ['day1'], param2: ['day3', 'day2', 'day0', 'day1'] };
console.log(match(object, object2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.4/lodash.min.js"></script>
Upvotes: 2
Reputation: 3425
_.isMatch will basically tell you if the second object is "contained" in the first.
var object = { 'a': 1, 'b': 2 };
_.isMatch(object, { 'b': 2 });
// => true
_.isMatch(object, { 'b': 1 });
// => false
(https://lodash.com/docs#isMatch)
It seems that you need _.isEqual
(https://lodash.com/docs#isEqual)
Upvotes: 1