I can't achieve to have two nested map
:
render() {
return (
<table className="table">
<tbody>
{Object.keys(this.state.templates).map(function(template_name) {
return (
<tr key={template_name}><td><b>Template: {template_name}</b></td></tr>
{this.state.templates[template_name].items.map(function(item) {
return (
<tr key={item.id}><td>{item.id}</td></tr>
)
})}
)
})}
</tbody>
</table>
)
}
This gives a SyntaxError: unknown: Unexpected token
.
How do you nest map
calls in JSX?
You need to wrap it inside an element.
Something like this (I've added an extra
tr
due to the rules of tables elements):Running Example (without a table):
I struggled for a while to get my nested map function to work only to discover that
what
youreturn
is critical. Make sure you are returning the second map itself and not just the final expected output:I think the problem is that the return type should be an array but not an object in React16. You could try like this below: