本文介绍了比较嵌套对象与JavaScript中的数组并返回键相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个嵌套对象obj1和obj2,我想将它们进行比较,然后递归返回一个对象,该对象的每个嵌套键都有一个类似相等的布尔值标志

I have two nested objects obj1 and obj2 and I want to compare them and the recursively return an object that for each nested key has a equality-like boolean flag

对于给定的 obj1 这样的

var obj1 = {
    prop1: "BAR",
    prop2: "foo",
    prop3: [
        {
            id: 1,
            prop4: "foo",
            prop5: "a"
        },
        {
            id: 2,
            prop4: "foo",
            prop5: "b"
        },
        {
            id: 3,
            prop4: "foo",
            prop5: "e"
        }
    ]
}

obj2 一样

var obj2 = {
    prop1: "FOO",
    prop2: "foo",
    prop3: [
        {
            id: 1,
            prop4: "bar",
            prop5: "b"
        },
        {
            id: 2,
            prop4: "foo",
            prop5: "a"
        },
        {
            id: 4,
            prop4: "foo",
            prop5: "e"
        }
    ],
    prop6: "new"
}

它应该返回

var equality = {
    prop1: false,
    prop2: true,
    prop3: [
        {
            id: 1,
            prop4: false,
            prop5: false
        },
        {
            id: 2,
            prop4: true,
            prop5: false
        },
        {
            id: 3,
            prop4: null,
            prop5: null
        },
        {
            id: 4,
            prop4: true,
            prop5: true
        }
    ],
    prop6: true
}

我必须比较两个对象,并为相同的值返回true.对于数组内部,我必须与ID(key)进行比较,并检查prop4,prop 5是否已更改,如果更改则返回false.对于obj 1中的数据,而obj 2中不存在的数据=>我们可以忽略者显示为null,如result(equality)所示对于obj 2中的数据,而obj 1中不存在的数据=>对于其中的所有道具都应标记为true.

I have to compare two objects and return true for same values. And for inside the array I have to compare with ID(key) and check if prop4, prop 5 have changed and return false if changed.For Data in obj 1 and not present in obj 2 => we can neglector show as null as shown in result(equality)For Data in obj 2 and not present in obj 1 => should be flagged as true for all props in it.

Nina Scholz在中比较嵌套对象的解决方案JavaScript和返回键的相等性帮助了我,但我面临的唯一问题是prop3嵌套字段,但我需要的格式与数组相同.

The solution given by Nina Scholz in Compare nested objects in JavaScript and return keys equality has helped me but only problem I am facing is for prop3 I am getting as nested fields but I need it as same formatting as the array.

如果有人得到正确的解决方案,这对我非常有帮助.我是JavaScript的新手,学习它会对我有很大帮助.

It would be very much helpful for me if any one get the right solution. I am new to JavaScript and learning it would help me a lot.

推荐答案

一些棘手的递归可以完成这项工作.

Some tricky recursion can do the job.

var obj1 = { prop1: 1, prop2: "foo", prop3: [{ number: 1, prop4: "foo", prop5: "a" }, { number: 2, prop4: "foo", prop5: "b" }] }
var obj2 = { prop1: 3, prop2: "foo", prop3: [{ number: 1, prop4: "bar", prop5: "b" }, { number: 2, prop4: "foo", prop5: "a" }, { number: 3, prop4: "foo", prop5: "e" }], prop6: "new" }

const isObject = v => v !== null && typeof v == "object";

function getDifference(x, y = x) {
    if (x === undefined) x = y;
    if (Array.isArray(x) && Array.isArray(y)) {
        const temp = [];
        for (let i = 0; i < (x.length + y.length) / 2; i++)
            temp.push(getDifference(x[i], y[i]))

        return temp;
    }
    if (isObject(x) && isObject(y)) {
        const temp = {};
        for (const key of new Set([...Object.keys(x), ...Object.keys(y)]))
            temp[key] = getDifference(x[key], y[key])

        return temp;
    }
    return x === y;
}

console.log(getDifference(obj1, obj2));

这篇关于比较嵌套对象与JavaScript中的数组并返回键相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:30