javascript lodash/underscore 检查一个对象是否包含另一个对象的所有键/值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29347584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:24:25  来源:igfitidea点击:

lodash/underscore check if one object contains all key/values from another object

javascriptunderscore.jslodash

提问by AlexStack

This is probably an easy question but I haven't been able to find an answer from the lodash API docs and Google.

这可能是一个简单的问题,但我无法从 lodash API 文档和 Google 中找到答案。

Let's assume I have an object like this:

假设我有一个这样的对象:

var obj = {
  code: 2,
  persistence: true
}

I want a function that I can pass a key/value pair and returns true if the key exists in my object and has the specified value:

我想要一个函数,我可以传递一个键/值对,如果键存在于我的对象中并且具有指定的值,则返回 true:

_.XXXX(obj, {code: 2});  //true
_.XXXX(obj, {code: 3});  //false
_.XXXX(obj, {code: 2, persistence: false});  //false
_.XXXX(obj, {code: 2, persistence: true});   //true

This is somehow like where()but for only one object.

这有点像where()但仅针对一个对象。

回答by Gruff Bunny

You could use a matcher:

您可以使用匹配器

var result1 = _.matcher({ code: 2 })( obj );  // returns true
var result2 = _.matcher({ code: 3 })( obj );  // returns false

with a mixin:

使用混合:

_.mixin( { keyvaluematch: function(obj, test){
    return _.matcher(test)(obj);
}});

var result1 = _.keyvaluematch(obj, { code: 2 });  // returns true
var result2 = _.keyvaluematch(obj, { code: 3 });  // returns false

Edit

编辑

Version 1.8 of underscore added an _.isMatchfunction.

下划线 1.8 版添加了_.isMatch函数。

回答by Calvin Cheng

https://lodash.com/docs#has

https://lodash.com/docs#has

var obj = {
  code: 2,
  persistence: true
};

console.log(_.has(obj, 'code'));

My bad for misunderstanding your requirement at first.

我一开始误解了你的要求。

Here's the corrected answer with _.somehttps://lodash.com/docs#some

这是https://lodash.com/docs#some的更正答案_.some

var obj = {
  code: 2,
  persistence: true
};

console.log( _.some([obj], {code: 2}) );
console.log( _.some([obj], {code: 3}) );
console.log( _.some([obj], {code: 2, persistence: false}) );
console.log( _.some([obj], {code: 2, persistence: true}) );

The trick is to cast the object you want to check as an Array so that _.somewill do its magic.

诀窍是将要检查的对象转换为 Array,这样就_.some可以发挥它的魔力。

If you want a nicer wrapper instead of having to manually cast it with [], we can write a function that wraps the casting.

如果你想要一个更好的包装器而不是手动转换它[],我们可以编写一个包装转换的函数。

var checkTruth = function(obj, keyValueCheck) {
  return _.some([obj], keyValueCheck);
};

console.log( checkTruth(obj, {code: 2}) );
... as above, just using the `checkTruth` function now ...

回答by jcubic

I don't think there is one single underscore function for that, but you can easily write one:

我不认为有一个单一的下划线函数,但您可以轻松编写一个:

function sameObject(ob1, ob2) {
   for (var key in ob2) {
      if (ob2[key] != ob1[key]) {
          return false;
      }
   }
   return true;
}