【发布时间】: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