gpt4 book ai didi

reactjs - 如何在 React 组件中迭代嵌套数组?

转载 作者:行者123 更新时间:2023-12-03 13:12:17 27 4
gpt4 key购买 nike

我有一个多维数组:一个数组results,包含 18 个数组row,每个数组包含 6 个数字。

我想将其呈现为表格。逻辑是:

results.each as (row)
<tr>
row.each as (number)
<td>number</td>
</tr>

但我不知道你如何在 JSX 中编写这个。

const Resultset = props => (
{props.rows.map(rows => {
<tr>
{rows.map(number => <td>{number}</td>)}
</tr>
})}
);

但这是不对的。这个过程是什么?如何嵌套 map 调用和插值?

最佳答案

一种方法

var arr = [ [ 2, 3, 4, 5, 6, 7 ],
[ 8, 9, 10, 11, 12, 13 ],
[ 14, 15, 16, 17, 18, 19 ],
[ 20, 21, 22, 23, 24, 25 ],
[ 26, 27, 28, 29, 30, 31 ],
[ 32, 33, 34, 35, 36, 37 ],
[ 38, 39, 40, 41, 42, 43 ],
[ 44, 45, 46, 47, 48, 49 ],
[ 50, 51, 52, 53, 54, 55 ],
[ 56, 57, 58, 59, 60, 61 ],
[ 62, 63, 64, 65, 66, 67 ],
[ 68, 69, 70, 71, 72, 73 ],
[ 74, 75, 76, 77, 78, 79 ],
[ 80, 81, 82, 83, 84, 85 ],
[ 86, 87, 88, 89, 90, 91 ],
[ 92, 93, 94, 95, 96, 97 ],
[ 98, 99, 100, 101, 102, 103 ],
[ 104, 105, 106, 107, 108, 109 ] ];

var Hello = React.createClass({
tablerows: function() {
return this.props.arr.map(rows => {
var row = rows.map(cell => <td>{cell}</td>);
return <tr>{row}</tr>;
});
},
render: function() {
return <table>{this.tablerows()}</table>;
}
});

ReactDOM.render(
<Hello arr={arr} />,
document.getElementById('container')
);

实际操作:https://jsfiddle.net/69z2wepo/30476/

关于reactjs - 如何在 React 组件中迭代嵌套数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35249979/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com