-1

My API hands me this array of objects:

[
 { id: 5, name: "foo" },
 { id: 7, name: "bar" }
]

I would like to filter out the ID keys and achieve this:

[5,7]

What would be considered best practice in this case?

Friso Hoekstra
  • 845
  • 2
  • 9
  • 24

1 Answers1

-1

Just use array.map:

var data = [
 { id: 5, name: "foo" },
 { id: 7, name: "bar" }
];

var res = data.map(({id}) => id);

console.log(res);
Faly
  • 13,291
  • 2
  • 19
  • 37