-2

I have an object that looks like this:

const data =  {
"supportPaneltext": [{
        "brand": "BMW",
        "paragraphList": [{
            "rowList": [
                "Europe, Middle East, Africa, Asia, Pacific"
            ]
        }]
    },
    {
        "brand": "Mercedez",
        "paragraphList": [{
                "rowList": [
                    "Europe, Middle East, Africa, Asia, Pacific"
                ]
            }]
    }]
}

I want to filter out the "brand" value to an array. The result would look like: ["BMW", Mercedez"]

I have tried to filter out by:

Object.values(data).filter((item) => item.brand) 

Without luck, What do I miss?

Arte
  • 397
  • 1
  • 4
  • 14
  • 1
    Why `filter()`? As the name suggests (and the [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) explains) `.filter()` returns elements from an array that fulfill a supplied predicate. Have a look at `.map()` – Andreas Sep 18 '21 at 15:44

1 Answers1

1

You can use .map() on your supportPaneltext

let result = data.supportPaneltext.map(a => a.brand);

const data = {
  "supportPaneltext": [{
      "brand": "BMW",
      "paragraphList": [{
        "rowList": [
          "Europe, Middle East, Africa, Asia, Pacific"
        ]
      }]
    },
    {
      "brand": "Mercedez",
      "paragraphList": [{
        "rowList": [
          "Europe, Middle East, Africa, Asia, Pacific"
        ]
      }]
    }
  ]
}

let result = data.supportPaneltext.map(a => a.brand);

console.log(result);
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
  • 1
    As already explained in the [duplicates here on SO](https://duckduckgo.com/?t=lm&q=javascript+array+of+objects+extract+property+site%3Astackoverflow.com&ia=web) – Andreas Sep 18 '21 at 15:46