-3

I have the below array:

[
    { id: 1, name: 'A' },
    { id: 2, name: 'B' }
]

How can I add a key-value pair in each array item as the best practice? For example:

[
    { id: 1, name: 'A', new: true },
    { id: 2, name: 'B', new: true }
]
jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
Luan Tran
  • 326
  • 1
  • 3
  • 13

1 Answers1

1

I would recommend using the map array method like this:

const newArray = oldArray.map((element) => ({...element, newKey: newValue}))
Peter Kusza
  • 141
  • 2
  • 8