Reputation: 109
Try to return field of array1
and another field of array2
, on comparison.
I have two array of objects (client and customer). I want to return client id and customer name where customer id is equal to client id.For this purpose I want to use map,filter,but can't figure out how can I use here below is my try,
let clientcontract=this.state.addclient.filter(client=>{
return(
this.state.customer.filter(cust=>{
return (
cust.id===client.id // comparing customer and client id
)
})
)
});
This approach is used to get the field where both id of customer and client are same but didn't know how I can get customer name and client id and return in client contract, as I am using filter first time so facing problem in it.
Upvotes: 3
Views: 482
Reputation: 42352
You can use some()
function inside the filter()
function. And finally to get the customer name, use a map()
function - see below:
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);
Upvotes: 1