gpt4 book ai didi

javascript - 从对象中获取数组差异的最佳方法是什么?

转载 作者:行者123 更新时间:2023-11-28 17:12:05 27 4
gpt4 key购买 nike

我有一个值数组:["1", "2", "3"],它本质上包含存储在该对象数组中的记录的引用:

[
{ id: 1, name: "John" },
{ id: 2, name: "Patrick" },
{ id: 3, name: "Jack" },
{ id: 4, name: "Paula" },
{ id: 5, name: "Sarah" }
]

我想从对象数组中返回丢失的引用,因此结果将是:4, 5。到目前为止,我取得的成果是从 html 中可用的所有 select 中获取第一个数组的所有选定值:

var selected_options = $('.options-picker')
.map(function() { return this.value}).get();

这将返回1,2,3。如何从对象数组 4, 5 中提取内容?

提前致谢。

最佳答案

使用filterincludes根据数组中的值检查对象 ID。

const data = [
{ id: 1, name: "John" },
{ id: 2, name: "Patrick" },
{ id: 3, name: "Jack" },
{ id: 4, name: "Paula" },
{ id: 5, name: "Sarah" }
];

const items = [1, 2, 3];

const out = data.filter(obj => !items.includes(obj.id));

console.log(out);

关于javascript - 从对象中获取数组差异的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54160278/

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