【问题标题】:TypeError: .json WEBPACK_IMPORTED_MODULE_2__.filter is not a function类型错误:.json WEBPACK_IMPORTED_MODULE_2__.filter 不是函数
【发布时间】:2019-12-01 02:50:50
【问题描述】:

当我尝试在我的代码selectedEmployee: employeeList.data.Table[0], 的这一行从 json 文件中获取数据时出现错误

TypeError: _employeeList_json__WEBPACK_IMPORTED_MODULE_2__.filter 是 不是函数

//App.js

    const filterEmployee = (searchText, maxResults) => {
  return employeeList.filter((employee) => {
    if (employee.data.Table.name.toLowerCase().includes(searchText.toLowerCase())) {
      return true;
    }
    return false;
  }).slice(0, maxResults);
}

var maxResults = 4;

export default class App extends React.Component {

    constructor(){
        super();
        this.state = {
            selectedEmployee: employeeList.data.Table[0],
            filteredEmployee: filterEmployee('', maxResults)
        }
    }

    onSearch = (event) => {
        this.setState({
            filteredEmployee: filterEmployee(event.target.value, maxResults)
        });
    }

    onEmployeeClick = (employee) => {
        this.setState({
            selectedEmployee: {name: employee.name, info: employee.info, contact: employee.contact}
        });
    }

    render() {
        return (
            <Col lg={8} md={7} sm={4} lgOffset={2}>
                <Col lg={6}>
                    <HomePage onSearch={this.onSearch} employeeData={this.state.filteredEmployee} onEmployeeClick={this.onEmployeeClick}/>
                </Col>  
                <Col lg={6}>
                    <EmployeePage selectedEmployee={this.state.selectedEmployee}/>
                </Col>
            </Col>
        );
    }
}

//我的json文件是这样的

{
    "data": {
      "Table": [
        {
          "id": "1001",
          "name": "Alez",
          "info": "Alez"
        },
        {
          "id": "1002",
          "name": "Baro",
          "info": "Alez"
        }

      ]
    }
  }

我想要完成的是使用不同的 .json 格式。

这些是原始的 json 文件格式

[
    { 
        "key": "t1",
        "data":{
            "name": "James",
            "info": "Software Development",
            "contact": {
                "office": "781-000-002",
                "mobile": "087-321-0292",
                "sms": "617-000-002",
                "email": "jtaylor@company.ie"
            }
        }
    }
]

我想改用这些 json 文件格式并更新我的代码

   {
    "data": {
      "Table": [
        {
          "id": "1001",
          "name": "Alez",
          "info": "Alez"
        },
        {
          "id": "1002",
          "name": "Baro",
          "info": "Alez"
        }

      ]
    }
  }

【问题讨论】:

    标签: javascript arrays json reactjs


    【解决方案1】:

    该错误与代码顶部的 filter 函数调用有关。你只能filter on an array。因此,您需要像使用 employeeList.data.Table 作为数组一样调用过滤器:

    const filterEmployee = (searchText, maxResults) => {
      return employeeList.data.Table.filter((employee) => {
        // returns true if condition is met, otherwise returns false
        return employee.name.toLowerCase().includes(searchText.toLowerCase());
      }).slice(0, maxResults);
    }
    

    然后在检查employee.name 是否包含searchText 时,您可以直接访问employee.name

    【讨论】:

    • 谢谢这里` selectedEmployee:employeeList[0].data,`我得到TypeError: Cannot read property 'data' of undefined
    • employeeList 不是数组,因此您不能使用 [0] 访问该变量中的任何项目,这仅适用于数组。相反,您可以使用employeeList.data.Table[0] 返回该数组中的第一个对象,因为employeeList.data.Table 是一个对象数组。
    • 如果没有看到更多代码,我不确定。随意创建一个新问题,这可能会更容易、更快地回答您的问题。
    • 另一件事我如何从另一个页面属性访问名称?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 2016-02-26
    • 2013-04-01
    • 2016-03-06
    • 2018-04-28
    • 2019-02-14
    相关资源
    最近更新 更多