-2

I have this array:

[
  {
    "id": "5e235b42a21b160a40c4a82f",
    "title": "category one"
  },
  {
    "id": "5e235b3ea21b160a40c4a82e",
    "title": "category two"
  }
]

And I want to convert it to the following array:

[
  "5e235b42a21b160a40c4a82f",
  "5e235b3ea21b160a40c4a82e"
]

Anyone have a simple suggestion?

Calvin Nunes
  • 6,376
  • 4
  • 20
  • 48
Nima Kaviyani
  • 103
  • 3
  • 13

1 Answers1

0

The best way to do this is by iterating over the Object values and taking each id value.

The simplest way of doing this is by using .map() like so:

const newArray = myArray.map(item => item.id);

This should take each item and form the id property into an array.

Tristan
  • 3,530
  • 3
  • 30
  • 39