Search code examples
javascriptarraysfind

How to get an object property value from an array using the value of another property


I want to get the value of a property from an array of objects using the value of another property let's say I have the following array:

const objArray = 
  [
    {car: 'BMW', price: 2000},
    {car: 'VW', price: 3500}, 
    {car: 'FORD', price: 4000}
 ]

Is it possible to access (get) the price using the car value ? Something like: objArray.car['BMW'].price ?


Solution

  • Is this meet your demand? In my opinion,the car must store in a variable or pass it as a parameter

    const objArray = 
      [
        {car: 'BMW', price: 2000},
        {car: 'VW', price: 3500}, 
        {car: 'FORD', price: 4000}
     ]
     
    let price = objArray.filter(a => a.car =='VW').map(e => e.price)[0]
    console.log(price)