简体   繁体   English

如何使用lodash或javascript匹配两个对象?

[英]How to do i match two object using lodash or javascript?

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. 我正在尝试使用lodash的isMatch函数在两个对象下面进行匹配,但它始终使我成真,因为我看到的是烫发对象的长度不同,并且与以下param数组中缺少的day2都不匹配,这是我尝试的方式。

 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. 如果数组包含所有值并且与所有键匹配,则它将以有序方式排列,则所有数组都应返回true,否则返回false。

You could use this plain JavaScript function: 您可以使用以下简单的JavaScript函数:

 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: 使用lodash,您可以在每个键/值对上使用difference

 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> 

_.isMatch will basically tell you if the second object is "contained" in the first. _.isMatch基本上会告诉您第二个对象是否在第一个中“包含”。

var object = { 'a': 1, 'b': 2 };

_.isMatch(object, { 'b': 2 });
// => true

_.isMatch(object, { 'b': 1 });
// => false

( https://lodash.com/docs#isMatch ) https://lodash.com/docs#isMatch

It seems that you need _.isEqual 似乎您需要_.isEqual

( https://lodash.com/docs#isEqual ) https://lodash.com/docs#isEqual

_.isEqual(value, other) _.isEqual(值,其他)

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, ie === 通过严格相等比较功能和DOM节点,即===

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. 它将返回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 它将返回false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM