javascript - 反应本机中的循环组件
问题描述
我想使用以下对象返回一个像表格这样的组件。但我必须只使用点数组。不需要头部标签。
const states = {
player: [
{
id: 1,
name: 'David',
points: [3, 4, 8, 2, 5, 3, 9, 5, -4, -6, 2]
},
{
id: 2,
name: 'John',
points: [3, 4, 8, 2, 5, 3, 9, 5, -4, -6, 2]
},
{
id: 3,
name: 'Sam',
points: [3, 4, 8, 2, 5, 3, 9, 5, -4, -6, 2]
},
{
id: 4,
name: 'Johanson',
points: [3, 4, 8, 2, 5, 3, 9, 5, -4, -6, 2]
}
]
}
我在 react-native-paper 中使用 DataTable。它具有这种结构;
<DataTable>
<DataTable.Header>
<DataTable.Title>Dessert</DataTable.Title>
<DataTable.Title>Calories</DataTable.Title>
<DataTable.Title>Fat</DataTable.Title>
</DataTable.Header>
<DataTable.Row>
<DataTable.Cell>Frozen yogurt</DataTable.Cell>
<DataTable.Cell>159</DataTable.Cell>
<DataTable.Cell>6.0</DataTable.Cell>
</DataTable.Row>
</DataTable>
解决方案
我会这样做:
编辑:由于您想在列而不是行中呈现点,我假设所有玩家都有相同数量的点并使用第一个玩家的点来呈现行。
const states = {
player: [
{
id: 1,
name: "David",
points: [3, 4, 8, 2, 5, 3, 9, 5, -4, -6, 2],
},
{
id: 2,
name: "John",
points: [3, 4, 8, 2, 5, 3, 9, 5, -4, -6, 2],
},
{
id: 3,
name: "Sam",
points: [3, 4, 8, 2, 5, 3, 9, 5, -4, -6, 2],
},
{
id: 4,
name: "Johanson",
points: [3, 4, 8, 2, 5, 3, 9, 5, -4, -6, 2],
},
],
};
function MyDataTable() {
return (
<DataTable>
{states.player[0].points.map((p, i) => (
<DataTable.Row key={p}>
{states.player.map((player) => (
<DataTable.Cell>{player.points[i]}</DataTable.Cell>
))}
</DataTable.Row>
))}
</DataTable>
);
}
推荐阅读
- c# - 使用 C# 中的虚拟 PDF 打印机以编程方式和静默方式打印 Word 文档
- laravel - Composer 无法完成 Laravel 的安装
- angular - 为动态更改组件更改 ngControl.valueAccessor
- angularjs - 如何用 AngularJS 嵌套和嵌入这个例子?
- arrays - 'fgets' 读取空格而不是读取输入的字符串。为什么?
- javascript - 使用 React Hooks 设置 aria-expanded 的布尔值
- mysql - InnoDB:数据字典中的表标志为 0,但文件 ./ibdata1 中的标志为 0x4000
- ios - TableViewCell 内 UIButton 的弹出框
- c# - Firebase 6.16.1 指定的强制转换无效 Unity
- python - OSMNX 无法识别任何属性“AttributeError: module 'osmnx' has no attribute”