gpt4 book ai didi

javascript - 使用键数组过滤对象数组[逆]

转载 作者:行者123 更新时间:2023-11-28 18:19:29 25 4
gpt4 key购买 nike

我有以下键数组:

var keys = [{userId: "333"}, {userId: "334"}]

这个对象数组:

var users = [
{id: "333", firstName: "", lastName: "", idCard: "", birthDate: ""},
{id: "334", firstName: "", lastName: "", idCard: "", birthDate: ""},
{id: "335", firstName: "", lastName: "", idCard: "", birthDate: ""},
{id: "336", firstName: "", lastName: "", idCard: "", birthDate: ""}
]

我需要通过使用我的keys Array过滤来更改我的users Array[相反结果]。

想不通,求所有答案! (js、jquery、 Angular )

有人吗?

最佳答案

您可以使用filter()find()

var users = [
{id: "333", firstName: "", lastName: "", idCard: "", birthDate: ""},
{id: "334", firstName: "", lastName: "", idCard: "", birthDate: ""},
{id: "335", firstName: "", lastName: "", idCard: "", birthDate: ""},
{id: "336", firstName: "", lastName: "", idCard: "", birthDate: ""}
]
var keys = [{userId: "333"}, {userId: "334"}]

var result = users.filter(function(o) {
return !keys.find(function(e) {
return e.userId == o.id
})
})

console.log(result)

关于javascript - 使用键数组过滤对象数组[逆],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40128001/

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