简体   繁体   English

比较两个对象的值的最有效方法是什么?

[英]What's the most efficient way to compare values of two objects?

Let's say I have two objects, which might have some properties in common.假设我有两个对象,它们可能有一些共同的属性。 The overall idea is that one is used as a schema to compare it to the other.总体思路是,将一个用作模式以将其与另一个进行比较。 I would like to compare values of common properties and construct a new object representing these comparisons of each individual property.我想比较常见属性的值并构造一个新对象来表示每个单独属性的这些比较。 In the result, any properties in common should have the value of comparing the source properties for equality (eg result.abc = (obj1.abc == obj2.abc) ).结果,任何共同的属性都应该具有比较源属性是否相等的值(例如result.abc = (obj1.abc == obj2.abc) )。 Any properties that exist on only one of the two objects would be copied to the result with their original values.仅存在于两个对象之一上的任何属性都将以其原始值复制到结果中。

For example, consider the following objects to compare:例如,考虑以下要比较的对象:

const schema = {
        num: 123,
        str: 'hello',
        nested: {
            num: 123,
            str: 'hello',
        },
    },

    doc = {
        nums: [1,2,3],
        str: 'hello',
        nested: {
            num: 123,
            str: 'world',
            foo: 'bar',
        },
    };

The comparison result should be:比较结果应该是:

{
    nums: [1,2,3], // obj2.nums
    num: 123,  // obj1.num
    str: true, // obj1.str == obj2.str
    nested: {
        num: true,  // obj1.nested.num == obj2.nested.num
        str: false, // obj1.nested.str == obj2.nested.str
        foo: 'bar', // obj2.nested.foo
    },
}

How would I perform this comparison in the most efficient time and space way?我将如何以最有效的时间和空间方式进行这种比较?

Currently I have this implementation:目前我有这个实现:

function deepCompare(obj1, obj2) {
    const validatedObject = {}

    Object.keys(obj1).forEach(e => {
        if(object[e].constructor.name === 'Object') {
            validatedObject[e] = deepCompare(obj1[e], obj2[e])
        } else {
            validatedObject[e] = obj1[e] === obj2[e]
        }
    })

    return validatedObject
}

Are there any ways to do this more efficiently without using JSON.stringify , because I might have property that is missing, but I still want to return the fact that the values are correct despite the schema of those objects?是否有任何方法可以在不使用JSON.stringify的情况下更有效地执行此操作,因为我可能缺少属性,但我仍然想返回values是正确的事实,尽管这些对象的架构是正确的?

Maybe for in or for of loop?也许for infor of循环? But how would I test their effectiveness?但是我将如何测试它们的有效性呢? It's okay for small objects, but sometimes they get really big and I want to compare them in the most efficient way possible.小物体没关系,但有时它们会变得非常大,我想以最有效的方式比较它们。

NOTE for editors.编辑注意

I understand that this might seem like a duplicate, but it in fact is not, here I'm looking for a most efficient way to compare values , iteratively, recursively or some other way, not objects themselves我知道这可能看起来像重复,但实际上并非如此,在这里我正在寻找一种最有效的方法来比较,迭代,递归或其他方式,而不是对象本身

the logic is if obj1 has a key and obj2 has the same key, we compare the values of those keys逻辑是如果obj1有一个键并且obj2有相同的键,我们比较这些键的值

Then you should use那么你应该使用

function compare(a, b) {
    if (a === b) return true;
    if (typeof a == 'object' && a != null && typeof b == 'object' && b != null) {
        return Object.keys(a).every(k =>
            !(k in b) || compare(a[k], b[k])
        );
    }
    return false;
}

I think your solution is good enough, it's only missing some default checks (you don't need to iterate through the objects keys if you have the same refference for example).我认为您的解决方案已经足够好了,它只是缺少一些默认检查(例如,如果您有相同的引用,则不需要遍历对象键)。

Also, there's this package that claims is the fastest deep equal checker.此外,还有这个包声称是最快的深度相等检查器。

在此处输入图像描述

You can look over the code to see if you missed any other condition (it's still a recursive solution for the objects, but there are some other data types treated there if you're interested).您可以查看代码以查看是否遗漏了任何其他条件(它仍然是对象的递归解决方案,但如果您感兴趣,还可以在那里处理一些其他数据类型)。

There is also this article if you prefer written material instead of code that has more checks before recursive calls如果您更喜欢书面材料而不是在递归调用之前有更多检查的代码,那么还有这篇文章

What you want to do is first find the common object keys.您要做的是首先找到公共对象键。 Then as you loop through the properties you want to return false as soon as you find a mismatch.然后,当您遍历属性时,您希望在发现不匹配时立即返回 false。 In the snippet below I'm using an every loop which will stop as soon as one iteration returns false .在下面的代码片段中,我使用了一个every循环,一旦一次迭代返回false就会停止。 If you return a value in the if block you do not need the else block.如果在if块中返回值,则不需要else块。

 function deepCompare(obj1, obj2) { const commonKeys = Object.keys(obj1).filter(x => Object.keys(obj2).includes(x)); let valid = true; commonKeys.every(e => { if (obj1[e].constructor.name === 'Object') { console.log('object', e, obj1[e], obj2[e]) valid = deepCompare(obj1[e], obj2[e]) return valid // true goes to next, false ends the .every loop } console.log('not object', e, obj1[e] === obj2[e] ? 'match' : 'mismatch') valid = obj1[e] === obj2[e] return valid // true goes to next, false ends the .every loop }) return valid // returns the first false or true if everything passes } const obj1 = { num: 123, str: 'hello', nested: { num: 123, str: 'hello', } } const obj3 = { num: 1233, str: 'hello' } const obj4 = { num: 123, str: 'hello', nested: { num: 123, str: 'hellure', } } console.log(deepCompare(obj1, obj1.nested)) console.log(deepCompare(obj1, obj3)) console.log(deepCompare(obj1, obj4))

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

相关问题 React useEffect():比较两个对象的 arrays 是否相等的最有效方法 - React useEffect() : Most efficient way to compare if two arrays of objects are equal 比较两个巨大的 arrays 对象的最有效方法 - The Most efficient way to compare two huge arrays of objects 比较 javascript 中的 2 个元素的最有效方法是什么? - What's the most efficient way to compare 2 elements in javascript? 检查两个物体是否发生碰撞和减速的最有效方法是什么? - What is most efficient way to check if two objects are colliding and decellerate on impact? 合并两个对象数组的最有效方法 - Most efficient way to merge two arrays of objects 转换对象模型的最有效方法是什么? - What is the most efficient way to transform model of objects? 合并日期最有效的方法是什么? - What's the most efficient way to combine dates? 比较两个对象中选定属性的最佳方法是什么 - what's the best way to compare selected properties in two objects 根据对象数组中的值对 DOM 元素进行排序的最有效方法是什么? - What is the most efficient way to sort DOM elements based on values in an array of objects? 搜索 JavaScript 对象列表并确定它们是否包含相同值的最有效方法是什么? - What is the most efficient way to search through a list of JavaScript objects and determine whether they contain the same values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM