-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,087
  • 3
  • 20
  • 46
Nima Kaviyani
  • 93
  • 1
  • 11

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 Wiley
  • 3,420
  • 2
  • 29
  • 39