1

Say I had

var people =[{name:"Jim", address:{number:"21",street:"park lane"}},
{name:"Karen",address:{number:"35", street:"oxford drive"}},
{name:"Bob",address:{number:65, street:"main avenue"}}]

and I wanted to get an array of just the address objects, is there a shorthand way?

I know I could just iterate over the people array and push the addresses onto a new array, but I imagine there must be an easier way.

blomster
  • 768
  • 2
  • 10
  • 27

1 Answers1

1

Maybe this:

people.map(function(x){return x.address;});

You are still iterating, but it is more concise.

M. Page
  • 2,694
  • 2
  • 20
  • 35