Search code examples
htmlnode.jsreactjsmongodbmern

How do i access the array property of this nested object using map function?


I have an object with array properties that I am getting from Mongodb database. The backend setup is correct because I am able to get the output in console. But I am making some mistake in displaying this data in the desired table format.

{Array.isArray(lights)
                ? Object.values(lights).map((light, key) => (
                    <TableRow
                      key={key}
                      sx={{ "&:last-child td, &:last-child th": { border: 0 } }}
                    >
                      <TableCell>{light.name}</TableCell>
                      <TableCell>{light.status}</TableCell>
                    </TableRow>
                  ))
                : null}

This is the structure of the object I am getting in consoleScreenshot of console.log

How to I get the values of name and status rendered in the table using map function?


Solution

  • Object.values ​​- not needed here:

    Array.isArray(lights) ? lights.map((light, key) => ( ...