简体   繁体   English

如何在 reactjs 中使用 map function 和渲染列表

[英]How to use map function and render list in reactjs

I was trying to load this array and render it as a table using map function how can I do it.我试图加载这个数组并使用 map function 将其呈现为表格我该怎么做。 The array response is from useState .数组响应来自useState

I have tried using list indexing and rendering the array as a table but the array changes its number of columns and rows dynamically so indexing does not work as the number of columns and rows keep on changing.我尝试使用列表索引并将数组呈现为表,但数组会动态更改其列数和行数,因此随着列数和行数不断变化,索引不起作用。

Sample image is shown below:示例图像如下所示: 在此处输入图像描述

what I have tried我试过的

 {arrayData.map(function (item) {
  return item;
 })}

and got output like并得到了 output 之类的在此处输入图像描述

let array = [
    [
14.203298568725586,
5.916393756866455,
15.5038480758667,
15.5038480758667,
15.5038480758667
],
[
20.115657806396484,
7.724868297576904,
19.88268280029297,
19.88268280029297,
19.88268280029297
],
[
14.476829528808594,
6.237934112548828,
13.77694320678711,
13.77694320678711,
13.77694320678711
],
[
14.203298568725586,
5.916393756866455,
15.5038480758667,
15.5038480758667,
15.5038480758667
],
[
14.203298568725586,
5.916393756866455,
15.5038480758667,
15.5038480758667,
15.5038480758667
],
]

The name of your array and the what you're trying to map do not match.您的阵列名称和您尝试的 map 不匹配。

So所以

let array

Should be:应该:

const arrayData

For:为了:

 {arrayData.map((item) => {
  return item;
 })}

To work.去工作。 Also note arrays should be updated with useState so if that's the reason you're using let .另请注意 arrays 应使用useState更新,因此如果这是您使用let的原因。 You don't mention any further code for the grid you're trying to do.您没有提及您尝试执行的网格的任何进一步代码。 Are you passing this to a component in the map or building everything when looping?您是将其传递给map中的组件还是在循环时构建所有内容?

You also have a multidimensional array so I'm unsure what your goal is when you only show one map.您还有一个多维数组,所以当您只显示一个 map 时,我不确定您的目标是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM