Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
How can I add a dynamic Id to array of object

suggest i have this object of nested array

let order={
a:1
b:2
service:[
1:{a:1,b:3},
2:{a:3,b:4}
]
}

so i want to insert object to service array with new dynamic id i tried that

let serviceOrder={a:5,b:6}
let id=uuid4(); //example 4
let newSer={...order.service,id:serviceOrder}
```

but i get this

service:[
1:{a:1,b:3},
2:{a:3,b:4},
id:{a:5,b:6}
]

whereas i want to get this

service:[
1:{a:1,b:3},
2:{a:3,b:4},
3:{a:5,b:6}
]

so how i can do this thank in advance to help

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

try this:

let id=uuid4(); //example 4
let newSer={...order.service,[id]:serviceOrder}

when you want to add dynamic key to object you can use [ ] and you can place your variable inside [ ]

about 4 years ago · Juan Pablo Isaza Report

0

You can also do it like this.

let newSer = {}
newSer[id] = serviceOrder
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!