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

202
Views
How to loop through array of objects and replace value es6

I have an array of objects

const array =[{ 
    "id": 1,
    "time": "2021-09-22T05:36:22.484Z"
},
 {​
   ​"id": 2,
   ​"time": "2021-10-22T03:25:26.484Z"
}]

I want to replace all the time values after converting to a timezone.

I am able to convert to timezones using

moment.tz("time","America/Toronto").format("YYYY-MM-DD HH:mm:ss)

but not sure how to loop through to replace the time value of every object.

So ideally, I would have something like:

const array =[{ 
    "id": 1,
    "time": "2021-09-22 15:00"
},
 {​
   ​"id": 2,
   ​"time": "2021-10-22T 12:00"
}]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Based on your data, you could simple use forEach and manipulate the time property as per your needs.

const array =[
  {  "id": 1, "time": "2021-09-22T05:36:22.484Z" }, 
  {  "id": 2, "time": "2021-10-22T03:25:26.484Z" },
]

array.forEach(x => {
  x.time = moment(x.time).utcOffset(90).format("YYYY-MM-DD HH:mm:ss")
})
about 4 years ago · Juan Pablo Isaza Report

0

You could use map() to loop over the array and create a new array with the updated data.

const array = [{
  "id": 1,
  "time": "2021-09-22T05:36:22.484Z"
}, {
  "id": 2,
  "time": "2021-10-22T03:25:26.484Z"
}]

const result = array.map(({id, time}) => {
  return {
    id,
    time: moment(time).utcOffset(90).format("YYYY-MM-DD HH:mm:ss")
  }
});
about 4 years ago · Juan Pablo Isaza Report

0

array=array.map(ar=>{
ar.time=  convertedtimezone
//convert ar.time into timezone and assign it to ar.time
return ar
)
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!