0

I have an object:

[
    {
        "id": 10,
        "name": "comedy"
    },
    {
        "id": 12,
        "name": "documentary"
    },
    {
        "id": 11,
        "name": "scifi"
    }
]

I need to take only the id values and add them into 1 array like so:

[ 10, 12, 13 ]

or

{ 10, 12, 13 }

I am trying:

const getIDs = value.map( ( { id } ) => ( {
  id: id
} ) );

But it creates:

[
    {
        "id": 10
    },
    {
        "id": 12
    },
    {
        "id": 11
    }
]
CyberJ
  • 1,018
  • 1
  • 11
  • 24
  • _“But it creates […]”_ — You return [`({ id: id })`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer). Why do you expect anything else? `{ 10, 12, 13 }` isn’t a valid literal. – Sebastian Simon Nov 17 '21 at 04:28

1 Answers1

1

Inside the map just return the id values instead of objects

Here is the quick solution

const values = [
    {
        "id": 10,
        "name": "comedy"
    },
    {
        "id": 12,
        "name": "documentary"
    },
    {
        "id": 11,
        "name": "scifi"
    }
]


const getIDs = values.map(({id}) => id);

console.log(getIDs)
sojin
  • 2,158
  • 1
  • 10
  • 18