'how to bind a name value to a for loop using moment-timezone

I have a list of time zones that has 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'}

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 can't tell which timezone each result belongs to. How do I add the timezone name to it, and push it to an array, so it looks like this?

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


Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jan