gpt4 book ai didi

javascript - 比较两个对象,同时过滤掉特定的键

转载 作者:行者123 更新时间:2023-11-28 17:21:32 25 4
gpt4 key购买 nike

我正在尝试找出最好的方法来做到这一点。假设我有两个对象

const baseObject = {
firstName: "John",
lastName: "Doe",
dob: "01/01/00",
siblings: []
}

const updatedObject = {
firstName: "Johnathan",
lastName: "Doe",
dob: "01/01/00",
siblings: []
}

我想将我的 baseObject 与我的 updatedObject 进行比较,同时省略 dob 和 sibling 以查找另一个 obj 中的任何差异和更改。

我一直在努力如何做到这一点。我玩过 .filter、.map 等,但没有任何基础。

注意:是的,[]确实很重要。

感谢您的帮助。

最佳答案

由于您想要比较有限键上的两个对象,因此您需要在 if 条件中特别提及每个键,或者需要为这些键维护单独的内存。如果排除键较少,则为包括键维护一个数组。

const baseObject = {
firstName: "John",
lastName: "Doe",
dob: "01/01/00",
siblings: []
}

const updatedObject = {
firstName: "Johnathan",
lastName: "Doe",
dob: "01/01/00",
siblings: []
}

// Excluding
function compareWithoutKeys(first = {}, second = {}, excludes = []) {
return Object.entries(first).every(([key, value]) =>
excludes.includes(key) ? true : second[key] === value
)
}
compareWithoutKeys(baseObject, updatedObject, ['dob', 'siblings']) // true/false

// Including
function compareWithKeys(first = {}, second = {}, includes = []) {
return Object.entries(first).every(([key, value]) =>
includes.includes(key) ? second[key] === value : true
)
}
compareWithKeys(baseObject, updatedObject, ['firstName', 'lastName']) // true/false

更新用于比较的不仅仅是字符串。

如果您想要比较的不仅仅是字符串,例如siblings(这是一个数组),您必须更新比较函数,您将检查值的类型,然后进行相应的比较。就像如果值是 Object 则比较每个键,或者如果值是 Array 则比较每个索引。像这样的东西:

function isEqual(first, second) {
const firstType = Object.prototype.toString.call(first)
const secondType = Object.prototype.toString.call(second)

if (firstType !=== secondType) {
return false
}

switch (expression) {
case '[object Array]': return first.every((value, index) => value === second[index])
case '[object Object]': return Object.entries(first).every((value, index) => value === second[index])
default: return first === second
}
}

// Excluding
function compareWithoutKeys(first = {}, second = {}, excludes = []) {
return Object.entries(first).every(([key, value]) =>
excludes.includes(key) ? true : isEqual(value, second[key])
)
}
// usage
compareWithoutKeys(baseObject, updatedObject, ['dob', 'siblings']) // true/false
compareWithoutKeys(baseObject, updatedObject, ['dob']) // true/false

// Including
function compareWithKeys(first = {}, second = {}, includes = []) {
return Object.entries(first).every(([key, value]) =>
includes.includes(key) ? isEqual(value, second[key]) : true
)
}
// usage
compareWithKeys(baseObject, updatedObject, ['firstName', 'lastName']) // true/false
compareWithKeys(baseObject, updatedObject, ['firstName', 'lastName', 'siblings']) // true/false

注意:这只会进行第一级比较,如果您想进行深层比较,例如其中每个键都是数组的对象数组,或者您必须使用递归函数或回退到lodash等库。

递归可能类似于以下内容,但我尚未测试以下代码。

function isEqual(first, second) {
const firstType = Object.prototype.toString.call(first)
const secondType = Object.prototype.toString.call(second)

if (firstType !=== secondType) {
return false
}

switch (expression) {
case '[object Array]': return first.every((value, index) => isEqual(second[index], value))
case '[object Object]': return Object.entries(first).every((value, index) => isEqual(second[index], value))
default: return first === second
}
}

关于递归比较的文章: https://gomakethings.com/check-if-two-arrays-or-objects-are-equal-with-javascript/

Lodash 的 isEqual: https://lodash.com/docs/4.17.10#isEqual

关于javascript - 比较两个对象,同时过滤掉特定的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52376420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com