arrays - React 中的过滤器方法在 map 可用时不起作用

标签 arrays reactjs filter

我在 React 中使用三元检查。如果某个项目存在,则会触发 .map 方法,如果不存在,则会触发 .filter 方法。 map 工作正常,但过滤器却不行。它抛出一个错误,指出对象不是有效的 React 子对象,并使用数组。然而,据我了解,它是一个数组......

 {test.searchTerm === ""
            ? test.users.map((item, index) => (
                <>
                  <tr>
                    <td>{index + 1}</td>
                    <td>{item.name.first + " " + item.name.last}</td>
                    <td>{item.email}</td>
                    <td>{item.gender}</td>
                    <td>{item.phone}</td>
                    <td>
                      <img src={item.picture.thumbnail} alt=""></img>
                    </td>
                  </tr>
                </>
              ))
            : test.users.filter((item, index) => {
              //console.log(test.users)
                if (
                  test.searchTerm === item.name.first ||
                  test.searchTerm === item.name.last ||
                  test.searchTerm === item.name.first + " " + item.name.last
                ) {

                  return (
                    <>
                      <tr>
                        <td>{index + 1}</td>
                        <td>{item.name.first + " " + item.name.last}</td>
                        <td>{item.email}</td>
                        <td>{item.gender}</td>
                        <td>{item.phone}</td>
                        <td>
                          <img src={item.picture.thumbnail} alt=""></img>
                        </td>
                      </tr>
                    </>
                  );
                }
              })}

最佳答案

在 JavaScript 中 filter用于根据条件创建一个新数组。您的过滤方法应该为匹配元素返回 true,否则返回 false。之后,您必须映射生成的过滤数组。过滤后,您仍然有一个对象数组。

{test.searchTerm === ""
            ? test.users.map((item, index) => (
                <>
                  <tr>
                    <td>{index + 1}</td>
                    <td>{item.name.first + " " + item.name.last}</td>
                    <td>{item.email}</td>
                    <td>{item.gender}</td>
                    <td>{item.phone}</td>
                    <td>
                      <img src={item.picture.thumbnail} alt=""></img>
                    </td>
                  </tr>
                </>
              ))
            : test.users.filter((item, index) => {
              //console.log(test.users)
                if (
                  test.searchTerm === item.name.first ||
                  test.searchTerm === item.name.last ||
                  test.searchTerm === item.name.first + " " + item.name.last
                ) {

                  return true;
                }
                return false
              }).map(item => (
                    <>
                      <tr>
                        <td>{index + 1}</td>
                        <td>{item.name.first + " " + item.name.last}</td>
                        <td>{item.email}</td>
                        <td>{item.gender}</td>
                        <td>{item.phone}</td>
                        <td>
                          <img src={item.picture.thumbnail} alt=""></img>
                        </td>
                      </tr>
                    </>
              ))}

关于arrays - React 中的过滤器方法在 map 可用时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60241365/

相关文章:

javascript - 在 React 中使用动画滚动到顶部按钮

javascript - 按多个键和值过滤,Javascript

javascript - Polymer 获取过滤器返回的对象的属性

c# - CodeDom 嵌套数组

java - 将对象转换为数组

reactjs - 在 react typescript 中指定文件上传事件的类型

javascript - AngularJS 过滤器表达式未更新

arrays - LISP 创建数组

c - 使用 C 将字符串的 csv 文件读取到 2D char* 数组

css - React-bootstrap 按钮动画将不起作用,按钮不会居中