0

I have below array of object.

items = [ { id: 0, title: "first item"}, { id: 1, title: "Second item"}, { id: 2, title: "third item"}];

What I want as another array of object from items object : idOfItems = [ { id: 0}, { id: 1}, { id: 2}];

2 Answers2

1

I suggest you use the map function.

const idOfItems = items.map(i => ({ id: i.id }));

This returns the array in the format you need.

0

use array.map

const idOfItems = items.map(item => ({id: item.id}))
andy mccullough
  • 9,070
  • 6
  • 32
  • 55