is there a way to get values from an array by fields at once?
for example there is an array of objects
const arr = [
{
name: 'tomato',
value: 20,
},
{
name: 'apple',
value: 44,
},
{
name: 'mango',
value: 36,
}
]
from this array I need to get all objects by name. And in a result a need 3 object. The one way is to find each object in an array by name
const tomato = arr.find(item => item.name === 'tomato')
const apple = arr.find(item => item.name === 'apple')
const mango = arr.find(item => item.name === 'mango')
is there others way how can I get object from an array by name? is it possible to do at one array iteration?
for...ofloop and store them in separate variables.const fruits = {tomato: 20, apple: 44, mango: 46};You can also map it to that form with areduceon your currentarr.tomatoitem? You just want to find the first one? So what if you use one iteration, and you give 3 names, then stop as soon as one is found for each name?