0

I have an array of objects as shown below

Array(2)
0: "104635ac-4b9a-3f7a-d4f8-e8badca83d58"
1: "547d57b7-ddc7-a481-95e4-ccfe2a6404a8"

I want to add two more properties one named productId and one name quantity to each element of this array of Objects.

The final outcome should be as follows.

  {
    "productId": "104635ac-4b9a-3f7a-d4f8-e8badca83d58",
    "quantity": 1,
  },
  {
    "productId": "547d57b7-ddc7-a481-95e4-ccfe2a6404a8",
    "quantity": 1,
  }

Can someone please let me know how to achieve this?

1
  • Duplicate targets found by Googling “site:stackoverflow.com js convert array of strings to array of objects”. Commented Sep 13, 2021 at 20:00

1 Answer 1

2

You can use Array.map to map through the array and construct an object:

const arr = ["104635ac-4b9a-3f7a-d4f8-e8badca83d58","547d57b7-ddc7-a481-95e4-ccfe2a6404a8"]

const result = arr.map(e => ({quantity: 1, productId: e}))

console.log(result)

Sign up to request clarification or add additional context in comments.

Comments