Javascript 使用 underscore.js 比较两个对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28632281/
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-08-23 02:09:00  来源:igfitidea点击:

Using underscore.js to compare two Objects

javascriptobjectunderscore.js

提问by Casey

I'm trying to compare two objects with underscore.

我正在尝试用下划线比较两个对象。

Object 1 (Filter)

对象 1(过滤器)

{
  "tuxedoorsuit":"tuxedoorsuit-tuxedo",
  "occasions":"occasions-wedding"
}

Object 2 (Properties)

对象 2(属性)

{
  "tuxedoorsuit":"tuxedoorsuit-tuxedo",
  "occasions":"occasions-wedding",
  "occasions":"occasions-prom",
  "product_fit":"product_fit-slim",
  "colorfamily":"colorfamily-black"
}

I want to return true when all items of Object 1 are found within Object 2. What would be the best underscore method to use for this?

当在对象 2 中找到对象 1 的所有项目时,我想返回 true。为此使用的最佳下划线方法是什么?

回答by thefourtheye

Edit:As per Arnaldo's comment, you can use isMatchfunction, like this

编辑:根据阿纳尔多的评论,您可以使用isMatch函数,如下所示

console.log(_.isMatch(object2, object1));

The description says,

描述说,

_.isMatch(object, properties)

Tells you if the keys and values in propertiesare contained in object.

_.isMatch(object, properties)

告诉您属性中的键和值是否包含在对象中。

If you want to iterate yourself, just use _.keysand _.every, like this

如果你想自己迭代,只需使用_.keysand _.every,就像这样

_.every(_.keys(object1), function(currentKey) {
    return _.has(object2, currentKey) &&
                    _.isEqual(object1[currentKey], object2[currentKey]);
});

Or the chained version,

或者链式版本,

var result = _.chain(object1)
    .keys()
    .every(function(currentKey) {
        return _.has(object2, currentKey) &&
            _.isEqual(object1[currentKey], object2[currentKey]);
    })
    .value();

If the result is true, it means that all the keys in object1are in object2and their values are also equal.

如果结果为true,则表示所有的键object1都在,object2并且它们的值也相等。

This basically iterates through all the keys of object1and checks if the value corresponding to the key in object1is equal to the value in object2.

这基本上遍历所有的键object1并检查对应于键输入object1的值是否等于输入的值object2

回答by pradeep gowda

To Compare two objects using underscore.js

使用 underscore.js 比较两个对象

 **isEqual :** _.isEqual(object, other) 

Performs an optimized deep comparison between the two objects, to determine if they should be considered equal.

在两个对象之间执行优化的深度比较,以确定它们是否应该被视为相等。

Ex :

前任 :

var stooge = {name: 'moe', luckyNumbers: [13, 27, 34]};
    var clone  = {name: 'moe', luckyNumbers: [13, 27, 34]};
     _.isEqual(stooge, clone)  
      Returns True

回答by Dmitri Algazin

Based on previous function created simple consoling out function:

基于之前的函数创建了简单的安慰函数:

var compareMe = function (obj1, obj2, parentKey) {
  parentKey = parentKey || '';
  _.each(_.keys(obj1), function (key) {
    if(_.isObject(obj1[key]) ) {
        compareMe(obj1[key], obj2[key], parentKey + key + '.')
    } else {
        if (!_.has(obj2, key) || !_.isEqual(obj1[key], obj2[key])) {
            console.log(parentKey + key, obj1[key], obj2[key]);
        }
    }
  })
};

And call like: compareMe(obj1, obj1)

并像这样调用: compareMe(obj1, obj1)