简体   繁体   中英

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:

                 {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 - [[]] .

Anybody can help me?

map return an array and if you nest them you will get nested arrays too.

If you change the first map to flatMap it will falten the results in to a single array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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