【问题标题】:React filtering Json array //TypeError: includes is not a function [duplicate]React过滤Json数组//TypeError:包含不是函数[重复]
【发布时间】:2020-05-15 21:23:20
【问题描述】:
this.state.records.filter(record => this.state.id == '' || record.id.includes(this.state.id))

    .map(record=> (

         <ProjectItem key={record.id} project={record} />

         ))

当我这样做时,我得到 TypeError: record.id.includes is not a function,includes is String func 但我的 record.id 无法识别

  constructor(props){

  super(props)

  this.state={

    records:[],

    id:''

   };

我正在从 axios.get() 获取记录[]

【问题讨论】:

标签: arrays reactjs filter filtering


【解决方案1】:

这意味着,records 数组不为空,但不知何故 id 未定义。例如:

records = [
  { score: 4.5 },
  ...
];

或者:

records = [4.5, 5.5, ...]

解决方案是先检查record.id 的有效性,然后再检查它是否包含this.state.id,如下所示:

this.state.records.filter(record =>
  this.state.id == '' || (record.id && record.id.includes(this.state.id))
);

更新: 如果上述方法仍然不起作用,可能是因为您正在 IE 上测试它。 includes 在 IE 中不起作用,因此您可能需要使用 indexOf

【讨论】:

  • 如果我这样做 .filter(d => (this.state.id == '' || d.id==this.state.id) 它可以接受,但必须是相同知道所以 id 已定义(我正在使用 Chrome)
  • 但是,d.id === this.state.id 即使未定义仍然有效。
  • d.id === this.state.id 它不工作
  • 我的意思是d.id === this.state.id 不会抛出错误,即使d.id 未定义。
  • 在前两种情况下,它会抛出错误 "cannot read property includes of undefined" 而不是 "record.id.includes is not a function" 。 OP需要添加includes的polyfill。
【解决方案2】:

您已经使用了两次记录变量。首先在 filter 函数的外部块中,然后在 map 函数的内部块中。因此编译器将无法识别要使用的记录变量,并会抛出错误。

您必须为 map 函数使用另一个变量,如下面代码中使用的那样。

function ProjectItem(props) {
		console.log("props => ",props)
  	return (
  	<div>
  	  <h1>{props.project.id}</h1>
  	</div>
    )
 
}


class Test extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      id:'a' , 
      records: [
                  {id:"a"}, 
                  {id:"b"}, 
                  {id:"c"}, 
                  {id:"d"}, 
                  {id:"e"}, 
                  {id:"f"}, 
                  {id:"g"}, 
                  {id:"h"}, 
                  {id:"i"}
                ]
     };
  }
  
  render() {

    return (
      <div>
       {this.state.records.filter(record => this.state.id == '' || record.id.includes(this.state.id))
        .map(rec => ( <ProjectItem  key={rec.id} project={rec} />) )
        }
      </div>
    )
  }
}

ReactDOM.render(<Test />, document.querySelector("#app"))
<div id="app"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

【讨论】:

  • 通常我使用另一个变量我在这里更改名称以便于理解
  • 你可以试试上面的代码。它正在工作。如果你运行上面的 sn -p 那么你可以清楚地看到上面的包含函数没有任何问题。它正在工作。
【解决方案3】:

首先includes()主要用于数组,在这里你在record.id上调用这个函数,我猜是一个字符串,所以你应该使用record.id.contains(x)

其次,在 js 中,标识 (===) 运算符的行为与相等 (==) 运算符的行为相同,只是没有进行类型转换,并且类型必须相同才能被视为相等。所以你应该在你的条件下使用身份运算符

【讨论】:

  • includes 在 String 类型上也有效。
  • id 包含或包含无关紧要。 => record.id.contains 不是一个函数现在我得到这个我有不同的问题
  • string.prototype.contains 不久前已被弃用,您应该使用它,并且您的答案中没有提供真正的解决方案。
猜你喜欢
  • 2017-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
相关资源
最近更新 更多