Is there a widely recognized technique to chain .map or .filter or .find functions in order to achieve this type of search?
Imagine an array of objects nested within another array of objects
customerGroups :
[
{
id: 1,
customers: [{
id: 1, // The same customer may be part of multiple groups
name: 'Jhon'
}],
},
{
id: 2,
customers: [{
id: 2,
name: 'Jhon'
}],
},
{
id: 3,
customers: [{
id: 2,
name: 'Doe'
}],
},
]
In a scenario where you have the customer's id and want to retrieve the customer's name, I aim to extract the customers array and utilize the Array.Find method
const idSearch = 1
const customerName = customers.find(({id})=>id==idSearch).name
I've been experimenting with
const customers = customerGroup.find(({ customer }) =>
customer.find(({ id }) =>idSearch === id),
)?.customers
const customerName = customers.find(({id})=>id==idSearch).name
I believe there must be an optimal approach for this, but I am currently too fatigued to determine it.
I've also attempted various strategies using .map to create a new array with all the customers, but haven't achieved satisfactory results yet.
I could potentially fetch that array from my Backend, but since I already have all the customers stored in memory, that would lead to unnecessary strain.