I'm trying to print JSON data (this) in React. I came to this:
const print = data.forEach(e => {
e.data.map(el => {
return(
<li>{el.account_id}</li>
)
})
});
return (
<div>
<ul>
{print}
</ul>
</div>
);
Yet it doesn't work (when i do console.log(el.account_id) it logs everything, but doesn't display the data in the ul). What am i doing wrong?
EDIT:
const print = data.map(e => {
return e.data.map(el => {
return(
<li>{el.account_id}</li>
)
})
});
return (
<div>
<ul>
{print}
</ul>
</div>
);
is the correct way to do it.
You're very close. forEach does not return anything. Use map.
You probably want something closer to this:
const print = data.map(record => (
<div key={record.meta.page}>
<h3>PAGE: {record.meta.page}</h3>
<ul>
{record.data.map(item => {
return (<li key={item.account_id}>{item.account_id}</li>)
})
}
</ul>
</div>
))
return (
<div>
{print}
</div>
)
const print = data.map(el => {
return(
<li>{el.account_id}</li>
)
})
return (
<div>
<ul>
{print}
</ul>
</div>
);
you dont need the forEach