Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

javascript - TypeError:.json WEBPACK_IMPORTED_MODULE_2 __。filter不是一个函数(TypeError: .json WEBPACK_IMPORTED_MODULE_2__.filter is not a function)

I am getting error when i try to get data from json file at this line in my code selectedEmployee: employeeList.data.Table[0],(当我在我的代码selectedEmployee: employeeList.data.Table[0],这一行尝试从json文件获取数据时,出现错误)

TypeError: _employeeList_json__WEBPACK_IMPORTED_MODULE_2__.filter is not a function(TypeError:_employeeList_json__WEBPACK_IMPORTED_MODULE_2 __。filter不是一个函数) //App.js(//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> ); } } //my json file looks like this(//我的json文件看起来像这样) { "data": { "Table": [ { "id": "1001", "name": "Alez", "info": "Alez" }, { "id": "1002", "name": "Baro", "info": "Alez" } ] } } What i want to accomplish is using a different .json format.(我要完成的工作是使用其他.json格式。) these was the orginal json file format(这些是原始的json文件格式) [ { "key": "t1", "data":{ "name": "James", "info": "Software Development", "contact": { "office": "781-000-002", "mobile": "087-321-0292", "sms": "617-000-002", "email": "[email protected]" } } } ] I want to use these json file format instead and update my code(我想改用这些json文件格式并更新我的代码) { "data": { "Table": [ { "id": "1001", "name": "Alez", "info": "Alez" }, { "id": "1002", "name": "Baro", "info": "Alez" } ] } }   ask by Kuku translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The error is related to the filter function call at the top of your code.(该错误与代码顶部的filter函数调用有关。)

You can only filter on an array .(您只能filter数组 。) Therefore you need to call filter like using employeeList.data.Table as your 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); } and then when checking if the employee.name includes the searchText , you can just access employee.name instead.(然后,当检查employee.name包含searchText ,您可以直接访问employee.name 。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...