3

比较时,尝试返回 的字段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 相同但不知道如何获取客户名称和客户 ID 并在客户合同中返回的字段,因为我第一次使用过滤器因此面临问题。

4

1 回答 1

1

您可以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);
于 2019-04-17T06:51:40.343 回答