Aaron
Aaron

Reputation: 55

How do I render current day in an array?

I am pulling some data from a REST API in my app and I am trying to parse the current day into the results using​ moment.

Here is an example of my code:

{restaurants.map((item, index) =>
<View key={item.id}>
<Text>
{`item.store_open_close.time.${currentDay.toLowerCase()}.opening_time`}</Text></View>
)}

but this is rendering, e.g.: item.store_open_close.time.thursday.opening_time

and not what I expected, e.g.:

10:00 am

I feel like I'm almost there? any help appreciated. Thanks!

PS - My data for restaurants looks like this

"store_open_close": {
        "enabled": true,
        "time": {
            "sunday": {
                "status": "close",
                "opening_time": "",
                "closing_time": ""
            },
            "monday": {
                "status": "open",
                "opening_time": "10:00 am",
                "closing_time": "7:00 pm"
            },
            "tuesday": {
                "status": "open",
                "opening_time": "10:00 am",
                "closing_time": "7:00 pm"
            },
            "wednesday": {
                "status": "open",
                "opening_time": "10:00 am",
                "closing_time": "7:00 pm"
            },
            "thursday": {
                "status": "open",
                "opening_time": "10:00 am",
                "closing_time": "7:00 pm"
            },
            "friday": {
                "status": "open",
                "opening_time": "10:00 am",
                "closing_time": "7:00 pm"
            },
            "saturday": {
                "status": "open",
                "opening_time": "10:00 am",
                "closing_time": "7:00 pm"
            }
        },
        "open_notice": "We are open",
        "close_notice": "Sorry we are closed"
    },

Upvotes: 1

Views: 55

Answers (1)

Paul Fitzgerald
Paul Fitzgerald

Reputation: 12129

You are looking for this:

<Text>{item.store_open_close.time[currentDay.toLowerCase()].opening_time}</Text>

Previously you were using a string literal and only expressing ${currentDay.toLowerCase()} in js, but you wanted the full line.

Upvotes: 4

Related Questions