javascript - 如何在映射时跳过 null? JSX

标签 javascript reactjs react-redux ternary-operator jsx

我是 ReactJs/Redux 和 JSX 的新手。 我有一个动态表,其中包含动态信息。

我的 map 有问题。我有 2 层 map :

<tbody>
  {
    data.map(row => (
      <tr key={`${row.type}`}>
        <td>
          <h5>{row.type}</h5>
        </td>
        <td>
          {
            row.text.map((name, indexText) => (
               <span key={row.text[indexText]} className="margin-right-10">
                 <Link
                   key={name}
                   role="button"
                   onClick={ () => this.getData(
                   this.state.le_id,
                   row.text[indexText][1],
                   row.type,
                   this.state.year,
                  )}>
                   {row.text[indexText][0]}
                 </Link>
               </span >
            ))
          }
        </td>
          <td>
              <Link className="btn btn-info btn-sm"
                    to={`/company/insurances/all-providers/${row.type}/${this.state.le_id}`}
              >
              {locales('create')}
              </Link>
          </td>
       </tr>
    ))
  }
</tbody>

以下是其实际效果的完整图片: image here

当我在过滤器中选择数组中某个地方为空的其他条件时,它会停止并显示错误:

list.js?6d8a:153 Uncaught (in promise) TypeError: Cannot read property 'map' of null
    at eval (eval at <anonymous> (http://localhost:3000/main-7a3d3e8ea9d6afcdba75.min.js:8972:1), <anonymous>:238:35)
    at Array.map (native)
    at ListsAccounting.render (eval at <anonymous> (http://localhost:3000/main-7a3d3e8ea9d6afcdba75.min.js:8972:1), <anonymous>:222:26)
    at eval (eval at <anonymous> (http://localhost:3000/main-7a3d3e8ea9d6afcdba75.min.js:16034:1), <anonymous>:796:21)
    at measureLifeCyclePerf (eval at <anonymous> (http://localhost:3000/main-7a3d3e8ea9d6afcdba75.min.js:16034:1), <anonymous>:75:12)
    at ReactCompositeComponentWrapper._renderValidatedComponentWithoutOwnerOrContext (eval at <anonymous> (http://localhost:3000/main-7a3d3e8ea9d6afcdba75.min.js:16034:1), <anonymous>:795:25)
    at ReactCompositeComponentWrapper._renderValidatedComponent (eval at <anonymous> (http://localhost:3000/main-7a3d3e8ea9d6afcdba75.min.js:16034:1), <anonymous>:822:32)
    at ReactCompositeComponentWrapper._updateRenderedComponent (eval at <anonymous> (http://localhost:3000/main-7a3d3e8ea9d6afcdba75.min.js:16034:1), <anonymous>:746:36)
    at ReactCompositeComponentWrapper._performComponentUpdate (eval at <anonymous> (http://localhost:3000/main-7a3d3e8ea9d6afcdba75.min.js:16034:1), <anonymous>:724:10)
    at ReactCompositeComponentWrapper.updateComponent (eval at <anonymous> (http://localhost:3000/main-7a3d3e8ea9d6afcdba75.min.js:16034:1), <anonymous>:645:12)

这是操作错误:image here

如何在映射时跳过 JSX 中的空值并获取所有元素,即使某些地方有空索引?

最佳答案

您还可以使用 Array#filter 删除 text 属性为 null 的每个条目。

data.filter(v => v.text !== null).map((name, textIndex) => { ...

关于javascript - 如何在映射时跳过 null? JSX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45375153/

相关文章:

javascript - javascript中箭头函数的流程?

javascript - 我可以保护原生 JavaScript 函数吗

javascript - Gatsby 功能组件事件处理传递 'this'

reactjs - 如何在 React 中使用 JWT token 实现私有(private)路由

javascript - 使用 onClick 函数检测正在单击的元素

javascript - React 组件中的两个提供者

javascript - Vaadin7 jQuery UI 集成

javascript - 我如何将菜单放在 Bootstrap 面板的左侧?

javascript - jQuery "Does not have attribute"选择器?

javascript - react _this2.setState 不是函数 - 可能的绑定(bind)问题