[英]Loop rendering with Arrays in Array in React
I have an array with arrays in it who look like this: [Array(4), Array(4), Array(1)]我有一个包含 arrays 的数组,看起来像这样:[Array(4), Array(4), Array(1)]
And i want to return the same render for every arrays, i tried something like this:我想为每个 arrays 返回相同的渲染,我尝试过这样的事情:
const articleRender = (array) => {
var index = 0;
var arrayLength = array.length;
for (index; index < arrayLength; index++) {
return (
<div>
{array[index].map((item, i) => {
return <li key={i}></li>;
})}
</div>
);
}
but the loop just make one iteration.但循环只进行一次迭代。
Thanks everyone.感谢大家。
You should wrap all the array iteration inside the return.您应该将所有数组迭代包装在返回中。
const articleRender = (array) => {
return (
<div>
array.map((item, index) => {
return (<div>
{item.map((subitem, i) => {
return <li key={i}></li>;
})}
</div>
)})
</div>
);
}
Because of the return on the first loop of the iteration.因为在迭代的第一个循环中返回。
for (index; index < arrayLength; index++) {
return ( <--- This return is stopping the iteration at index = 0.
<div>
{array[index].map((item, i) => {
return <li key={i}></li>;
})}
</div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.