通过使用地图过滤器与对象数组进行比较来返回字段



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

我有两个对象数组(客户端和客户(。我想返回客户 ID 和客户名称,其中客户 ID 等于客户端 ID.为此,我想使用地图,过滤器,但不知道如何使用这里这是我的尝试,

       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 并在客户合同中返回,因为我第一次使用过滤器,所以遇到问题。

您可以在filter()函数中使用some()函数。最后,要获取客户名称,请使用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);

最新更新