[英]ReactJS undefined arr.map
I´m fetching data from API and then using arr.map() to filter the data* ( API returns unecessary properties ) 我正在从API中获取数据,然后使用arr.map()过滤数据*(API返回了不必要的属性)
This is working: 这正在工作:
fetchTable() { const url = 'http://localhost:8000/issues/assigned/'; const value = this.state.selectedOption; const string = url+value; fetch(string) .then(function(response) { return response.json(); }) .then((myJson) => this.setState({data: myJson.issues})); } const filteredResult1 = this.state.data.map(item => ( { name: item.fields ? item.fields.assignee.name : '', id: item.id, key: item.key, timespent: item.fields ? this.timeConvert(item.fields.timespent) : '', project: item.fields ? item.fields.project.name : '', status: item.fields ? item.fields.status.name : '', created: item.fields ? item.fields.created : '', resolution: item.fields ? item.fields.resolutiondate : '' } ));
But now i need to setState multiple times, soo i can render a table for each array with the filtered Data, but the result from map is allways undefined! 但是现在我需要多次setState,所以我可以为每个数组渲染一个表,其中包含过滤后的数据,但是map的结果始终是不确定的!
.then((myJson) => this.setState({data: [...this.state.data, myJson.issues]}));
This is NOT working: 这不起作用:
fetchTable() { const url = 'http://localhost:8000/issues/assigned/'; const value = this.state.selectedOption; const string = url+value; fetch(string) .then(function(response) { return response.json(); }) .then((myJson) => this.setState({data: [...this.state.data, myJson.issues]})); } var result = this.state.data.map((item)=>{ return {id:item.id, example: item.example}; }); console.log(result)
let array1 = [ 1,2,3 ]; let array2 = [ 4,5,6 ] // ...array1 is destructuring array1 // and which leads to an unexpected result console.log([...array1, array2]);
Change this line 更改此行
.then((myJson) => this.setState({data: [...this.state.data, myJson.issues]}));
to this 对此
.then((myJson) => this.setState({
data: this.state.data.concat(myJson.issues)
}));
You can find in the code attached that the resulted array is not what you expect. 您可以在所附的代码中找到结果数组不是您期望的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.