gpt4 book ai didi

json - 在 typescript 中获取两个对象数组之间不匹配的对象

转载 作者:搜寻专家 更新时间:2023-10-30 21:26:43 26 4
gpt4 key购买 nike

我有两个对象数组:

first = [
{ "id": "15", "name": "raza" },
{ "id": "1", "name": "sahir" },
{ "id": "54", "name": "ayyan" },
{ "id": "3", "name": "tahir" },
];

second = [
{ "id": "15", "name": "razi" },
{ "id": "3", "name": "qasim" },
{ "id": "1", "name": "taha" },
];

我想根据它们的 id 从“第一个”数组中获取不匹配的对象,比如

const result = this.first.filter(e => this.second.some(({id}) => e.id ==id ));

它给了我匹配的对象,但我想得到不匹配的对象。

最佳答案

是这样的吗?

first = [
{ "id": "15", "name": "raza" },
{ "id": "1", "name": "sahir" },
{ "id": "54", "name": "ayyan" },
{ "id": "3", "name": "tahir" },
];

second = [
{ "id": "15", "name": "razi" },
{ "id": "3", "name": "qasim" },
{ "id": "1", "name": "taha" },
];

unmatched = first.filter(item => !second.some(_item => _item.id === item.id));

console.log(unmatched);

关于json - 在 typescript 中获取两个对象数组之间不匹配的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53853397/

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