Javascript 通过使用映射过滤器与对象数组进行比较返回字段

Javascript 通过使用映射过滤器与对象数组进行比较返回字段,javascript,arrays,reactjs,ecmascript-6,es6-map,Javascript,Arrays,Reactjs,Ecmascript 6,Es6 Map,比较时,尝试返回array1的字段和array2的另一个字段 我有两个对象数组(客户机和客户机)。我想返回客户id和客户名称,其中客户id等于客户id。为此,我想使用map、filter,但不知道如何使用下面的示例 let clientcontract=this.state.addclient.filter(client=>{ return( this.state.customer.filter(cust=>{

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

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

但是,当条件为true时,如何在clientcontract中返回客户名称和客户id?
clientcontract
将是一个来自
addclient
的值数组,然后满足条件-也许您需要交换
this.state.addclient
this.state.customer
around@MuhammadNabeel你会得到一个过滤列表在客户的
addclient
元素中…正如JaromandaX所说,如果您想要获得客户,您只需切换
addclient
customer
…是的,我得到了客户筛选结果,例如customer-{id:1,name:“abc”,age:4},但我只想返回name=“abc”从筛选结果和客户端id中获取,并将其与clientcontract进行比较,然后如何映射筛选器?