gpt4 book ai didi

javascript - 这段代码如何查找两个对象数组之间的差异?

转载 作者:太空宇宙 更新时间:2023-11-03 23:11:50 36 4
gpt4 key购买 nike

我已经根据 SO 的答案测试了这段代码,它基本上只返回数组 1 中存在但数组 2 中不存在的项目。但我不知道这是如何工作的,有人可以解释一下吗?

const arrayOne = [ 
{ value: "4a55eff3-1e0d-4a81-9105-3ddd7521d642", display: "Jamsheer" },
{ value: "644838b3-604d-4899-8b78-09e4799f586f", display: "Muhammed" },
{ value: "b6ee537a-375c-45bd-b9d4-4dd84a75041d", display: "Ravi" },
{ value: "e97339e1-939d-47ab-974c-1b68c9cfb536", display: "Ajmal" },
{ value: "a63a6f77-c637-454e-abf2-dfb9b543af6c", display: "Ryan" },
];

const arrayTwo = [
{ value: "4a55eff3-1e0d-4a81-9105-3ddd7521d642", display: "Jamsheer"},
{ value: "644838b3-604d-4899-8b78-09e4799f586f", display: "Muhammed"},
{ value: "b6ee537a-375c-45bd-b9d4-4dd84a75041d", display: "Ravi"},
{ value: "e97339e1-939d-47ab-974c-1b68c9cfb536", display: "Ajmal"},
];

const results = arrayOne.filter(({ value: id1 }) => !arrayTwo.some(({ value: id2 }) => id2 === id1));

console.log(results);

我认为 { value: id1 } 是解构赋值,但它似乎交换了键和值???和id2 === id1有什么关系?

最佳答案

  1. { value: id1 } 将属性分配给具有其他名称的变量。

例如:

({ a:id, b } = { a: 10, b: 20 });
console.log(id); // 10
console.log(a); // "Uncaught ReferenceError: a is not defined"
2. 将 value分配给 id1id2后,现在我们可以检查 arrayOne中的 id1arrayTwo中的 id2

const arrayOne = [ 
{ value: "4a55eff3-1e0d-4a81-9105-3ddd7521d642", display: "Jamsheer" },
{ value: "644838b3-604d-4899-8b78-09e4799f586f", display: "Muhammed" },
{ value: "b6ee537a-375c-45bd-b9d4-4dd84a75041d", display: "Ravi" },
{ value: "e97339e1-939d-47ab-974c-1b68c9cfb536", display: "Ajmal" },
{ value: "a63a6f77-c637-454e-abf2-dfb9b543af6c", display: "Ryan" },
];

const arrayTwo = [
{ value: "4a55eff3-1e0d-4a81-9105-3ddd7521d642", display: "Jamsheer"},
{ value: "644838b3-604d-4899-8b78-09e4799f586f", display: "Muhammed"},
{ value: "b6ee537a-375c-45bd-b9d4-4dd84a75041d", display: "Ravi"},
{ value: "e97339e1-939d-47ab-974c-1b68c9cfb536", display: "Ajmal"},
];

const results = arrayOne.filter(
({ value : id1 }) => !arrayTwo.some(
({ value: id2 }) => id2 === id1)
);
console.log(results);

相同的解决方案

const results = arrayOne.filter(
item1 => !arrayTwo.some( item2 => item1.value === item2.value)
);
console.log(results);

const arrayOne = [ 
{ value: "4a55eff3-1e0d-4a81-9105-3ddd7521d642", display: "Jamsheer" },
{ value: "644838b3-604d-4899-8b78-09e4799f586f", display: "Muhammed" },
{ value: "b6ee537a-375c-45bd-b9d4-4dd84a75041d", display: "Ravi" },
{ value: "e97339e1-939d-47ab-974c-1b68c9cfb536", display: "Ajmal" },
{ value: "a63a6f77-c637-454e-abf2-dfb9b543af6c", display: "Ryan" },
];

const arrayTwo = [
{ value: "4a55eff3-1e0d-4a81-9105-3ddd7521d642", display: "Jamsheer"},
{ value: "644838b3-604d-4899-8b78-09e4799f586f", display: "Muhammed"},
{ value: "b6ee537a-375c-45bd-b9d4-4dd84a75041d", display: "Ravi"},
{ value: "e97339e1-939d-47ab-974c-1b68c9cfb536", display: "Ajmal"},
];

// The same solution
const results = arrayOne.filter(
item1 => !arrayTwo.some( item2 => item1.value === item2.value)
);
console.log(results);

关于javascript - 这段代码如何查找两个对象数组之间的差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60272394/

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