1

I have the following object:

var people = [
    {
        "name": "James",
        "work": "Shake Shack"
    },
    {
        "name": "Stanley Hudson",
        "work": "IBM"
    }
]

Is there an easy way built into Javascript to allow me to get a list of values by their key?

For exemple if I want a list like ["Shake Shack", "IBM"], which is all the values in the array associated with the work key, how can I do?

I know how to do this manually with a loop but I'd really like to know if there's some built-in functionality in Javascript that could do this for me.

Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204
Shisui
  • 1,051
  • 1
  • 8
  • 23

1 Answers1

1

Try using Array.prototype.map :

var people = [
    {
        "name": "James",
        "work": "Shake Shack"
    },
    {
        "name": "Stanley Hudson",
        "work": "IBM"
    }
]

console.log(people.map((one) => one.work))
Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204