Home > front end >  how to bind a name value to a for loop using moment-timezone
how to bind a name value to a for loop using moment-timezone

Time:03-16

i have a list of time zones been populated as a query as follows

async load(){

this.usertimezones= await this.query(gets my query result)
}

this is what the usertimezones looks like

0:{userId: '0b8502d0-0b65-4092-bc4e-5c491acc7771', timezone: 'Africa/Bamako', isActive: true, id: '69c6051b-5b8b-43cf-b771-7884b6eb5c01'}
1: {userId: '0b8502d0-0b65-4092-bc4e-5c491acc7771', timezone: 'America/Araguaina', isActive: true, id: 'b609808d-3002-422a-810c-8d3e17c972bc'}

so for each timezone i pass it as follows

    async load(){
    
    this.usertimezones= await this.query(gets my query result)
this.usertimezones.forEach(x => {
            let test = Mo().tz(x.timezone).format('MMMM Do YYYY, h:mm a');
            console.log(test);
        })
    }

and then the output is as follows

March 15th 2022, 7:53 pm
March 15th 2022, 4:53 pm

however now i cant tell which one it belongs to, how do i add the timezone name to it and push it to an array so it looks like

Africa/Bamako March 15th 2022, 7:53 pm
  America/Araguaina  March 15th 2022, 4:53 pm

CodePudding user response:

You can re-use x.timezone in your forEach function. Like so:

const result = [];
this.usertimezones.forEach(x => {
    const time = Mo().tz(x.timezone).format('MMMM Do YYYY, h:mm a');
    const str = x.timezone   ' '   test;
    console.log(str);
    result.push(str);
})

The above code will log out the desired output, as well as pushing it into the result array.

  • Related