【问题标题】:React JS Filtering ARRAY //includes is not a function [duplicate]React JS过滤ARRAY //includes不是一个函数[重复]
【发布时间】:2020-01-30 12:02:07
【问题描述】:

我得到的 include 不是函数错误,并且已经在 VS Code 上 record.id 无法识别 .includes()

我不知道问题出在哪里?

import React, { Component } from 'react'
import axios from "axios"

export default class Tablo extends Component {
     constructor(props){
    super(props)
    this.state={
      records:[],
      id:1
    }
}

render() {
    return (
        <div>
        {
            this.state.records.filter(record => record.id.includes(this.state.id))
            .map(rec => (
                <li> {rec.shop}</li>
            ))
        }

        </div>
    )
}
componentDidMount() { 
    axios.get('http://localhost:8080/kayitlar').then(res => {
      console.log(res);
      this.setState({records:res.data});
    });
}

【问题讨论】:

  • 你的记录是什么。可以附上截图吗?
  • 您的records 数据是什么样的? includes 是一个数组函数,因此 records[n].id 必须是数组才有意义。请添加您正在使用的数据
  • .includes() 是 String 和 Array 数据类型的一部分
  • 如果 records[x]===1 那么包含将不起作用 - 那么您需要 this.state.records.includes(this.state.id)
  • 我在上面导入我的记录照片

标签: javascript reactjs


【解决方案1】:

includes() 方法判断一个字符串是否包含指定字符串的字符。

我认为你是在申请号码,所以给出错误。

【讨论】:

    【解决方案2】:

    看,includes 仅用于字符串数组或字符串比较,如果一个字符串包含另一个字符串的一部分。

    基于此,您有两种选择来解决您的问题

    1. 检查是否设置了record.id,其类型是字符串还是数组
    2. 假设 id 应该是唯一的,比较它们的最佳方法是使用 == 运算符

    【讨论】:

      猜你喜欢
      • 2018-05-26
      • 1970-01-01
      • 2019-09-23
      • 2017-11-10
      • 2020-05-15
      • 2018-12-24
      • 2019-12-03
      • 2020-11-01
      • 2020-04-17
      相关资源
      最近更新 更多