gpt4 book ai didi

javascript - 通过使用 Map Filter 与对象数组进行比较来返回字段

转载 作者:行者123 更新时间:2023-11-29 15:10:01 24 4
gpt4 key购买 nike

比较时,尝试返回 array1 的字段和 array2 的另一个字段。

我有两个对象数组(客户和客户)。我想返回客户 ID 和客户名称,其中客户 ID 等于客户 ID。为此,我想使用 map、filter,但不知道如何使用下面是我的尝试,

       let clientcontract=this.state.addclient.filter(client=>{
return(
this.state.customer.filter(cust=>{
return (
cust.id===client.id // comparing customer and client id
)
})
)
});

此方法用于获取客户和客户 ID 相同的字段,但不知道如何获取客户名称和客户 ID 并在客户契约(Contract)中返回,因为我是第一次使用过滤器所以面临问题

最佳答案

您可以使用 some() filter() 函数中的函数。最后,要获取客户名称,请使用 map() 函数 - 见下文:

let clientcontract=this.state.customer.filter(cust => {
return this.state.addclient.some(client => {
return cust.id === client.id // comparing customer and client id
});
}).map(cust => cust.name);

关于javascript - 通过使用 Map Filter 与对象数组进行比较来返回字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55721512/

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