这段查找两个对象数组之间差异的代码是如何工作的?
How this code to find difference betwen two array of objects works?
我已经从 SO 的回答中测试了这段代码,它基本上 returns 只有数组 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 }
是解构赋值,不过好像调换了key和value???和id2 === id1
有什么关系?
{ value: id1 }
将 属性 分配给另一个名称的变量。
例如:
({ a:id, b } = { a: 10, b: 20 });
console.log(id); // 10
console.log(a); // "Uncaught ReferenceError: a is not defined"
2. 将value
分配给id1
& id2
后,现在我们可以检查arrayOne
中的id1
和[=21=中的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);
我想说理解它的最好方法是使用一些老式的 vanilla JS 进行重构:
const results = arrayOne.filter(objectOne => {
return !arrayTwo.some(objectTwo => {
return objectOne.value === objectTwo.value;
})
});
这里我没有使用对象解构,也没有省略return
关键字,这让事情更清楚了。
我已经从 SO 的回答中测试了这段代码,它基本上 returns 只有数组 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 }
是解构赋值,不过好像调换了key和value???和id2 === id1
有什么关系?
{ value: id1 }
将 属性 分配给另一个名称的变量。
例如:
({ a:id, b } = { a: 10, b: 20 });
console.log(id); // 10
console.log(a); // "Uncaught ReferenceError: a is not defined"
value
分配给id1
& id2
后,现在我们可以检查arrayOne
中的id1
和[=21=中的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);
我想说理解它的最好方法是使用一些老式的 vanilla JS 进行重构:
const results = arrayOne.filter(objectOne => {
return !arrayTwo.some(objectTwo => {
return objectOne.value === objectTwo.value;
})
});
这里我没有使用对象解构,也没有省略return
关键字,这让事情更清楚了。