7

so in Javascript I have an array structured like this:

car = [{id: "1", brand: "Opel"}, {id: "2", brand: "Haima"},{id: "3", brand: "Toyota"}]

How can I get the values of all the brands only.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 10
    `car.map(item => item.brand)` – Endless Sep 27 '18 at 20:04
  • 1
    It depends on what you want to do with it. 1. A simple solution if you just want to log it to console car.forEach(car => console.log(car.brand)); Iterate over your car object with forEach. 2. If you want to collect it in an array for further manipulation const brands = car.map(car => car.brand); Map returns a new array of brand in that case. – gr4viton Sep 27 '18 at 20:10

2 Answers2

11

This should help:

const car = [{id: "1", brand: "Opel"}, {id: "2", brand: "Haima"},{id: "3", brand: "Toyota"}];

const brands = car.map(({ brand }) => brand);
Juan Elfers
  • 770
  • 7
  • 13
6

You can use array map to return all brands. Look at the following use of map.

car.map(o => o.brand)
Shubham Gupta
  • 2,596
  • 1
  • 8
  • 19