简体   繁体   English

为什么 map 使用两次时 typescript (tsx) 不返回 html?

[英]Why typescript (tsx) doesn't return html when map is using twice?

I've made array with chunks using this method:我使用这种方法制作了带有块的数组:

  const newArray: Array<Array<any>> = [];

  for (let i = 0; i < codeData?.length; i++) {
    if (i % 3 === 1) {
      newArray.push([codeData[i - 1], codeData[i], codeData[i + 1]].filter(el => el !== undefined));
    }

  }

But when I try to map it twice It doesn't return HTML , but when I set the value of the last map to the console it returns what I need:但是当我尝试 map 它两次它不会返回HTML ,但是当我将最后一个 map 的值设置到控制台时,它会返回我需要的内容:

                 {newArray
                ?.slice(
                  page * rowsPerPage,
                  page * rowsPerPage + rowsPerPage
                )
                ?.map((arr: any) => {
                  arr
                    .filter((element) => element !== undefined)
                    .map((row: any) => {
                      return (
                        <TableRow
                          role="checkbox"
                          key={row.id}
                          className={classes.row}
                        >
                          {row.id}
                        </TableRow>
                      );
                    });
                })}

I guess it happened because typescript, as for as I know, has this dynamic - [] but when I'm using map twice it has - [[]] .我想这是因为 typescript,据我所知,有这个动态 - []但是当我使用 map 两次它有 - [[]]

Anybody can help me?任何人都可以帮助我吗?

map return an array and if you nest them you will get nested arrays too. map返回一个数组,如果你嵌套它们,你也会得到嵌套的 arrays。

If you change the first map to flatMap it will falten the results in to a single array.如果您将第一个map更改为flatMap ,它会将结果折叠成单个数组。

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

相关问题 为什么 Typescript 在可能返回 object 类型被明确定义时不能正确推断返回值? - Why doesn't Typescript infer return values correctly when possible return object types are specifically defined? 打字稿:Map.has 没有返回正确的值 - Typescript: Map.has doesn't return correct value 使用 TSX 语法时如何在 HTML 跨度中使用 href? - How to use href in HTML span when using TSX syntax? 为什么Array#map不返回正确的数组? - Why doesn't Array#map return the correct array? 使用HTML5地理位置(使用Meteor js)时,Firefox不显示Google地图 - Google map doesn't show on Firefox when using HTML5 geolocation (using Meteor js) 当我使用括号表示法添加键/值对时,为什么地图大小属性没有更新? - Why doesn't the Map size property update when I add a key/value pair using bracket notation? 为什么我在使用 React useReducer 时不能返回之前 state 的 map - Why can't I return map of previous state when using React useReducer 单击两次时.focus()不聚焦 - .focus() doesn't focus when clicked twice 当我使用HTML dom动态添加动画时,为什么SMIL动画不起作用 - why doesn't SMIL animation work when I add it dynamically using HTML dom 为什么接口中的 Typescript 返回类型 void 在实现中不会触发错误? - Why Typescript return type void in interface doesn't trigger error in implementation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM