简体   繁体   English

ReactJS未定义的arr.map

[英]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.

相关问题 类型错误:arr.map 不是 function - TypeError: arr.map is not a function TypeError: arr.map is not a function in js, and nextjs - TypeError: arr.map is not a function in js, and nextjs 在 typescript 中使用 arr.map(elem => dict[elem]) 时如何返回 null 而不是 undefined? - How to return null instead of undefined when using arr.map(elem => dict[elem]) in typescript? 如何修复Javascript中的“ TypeError:arr.map不是函数”错误 - How to fix ‘TypeError: arr.map is not a function’ error in Javascript 对于没有值的数组,Arr.map 与 Array.from 不同 - Arr.map differs from Array.from for an array with no values 如何从 arr.map(elem => dict[elem]) 中提取 function? - How to extract a function out of arr.map(elem => dict[elem])? arr.map(Number) 其中 map 没有 arguments,只有数字 function? - arr.map(Number) where map has no arguments, only Number function? 在地图函数Reactjs中未定义“ this” - “this” is undefined inside map function Reactjs 带有axios的reactjs“无法读取未定义的属性'map'” - reactjs with axios “Cannot read property 'map' of undefined`” 无法读取未定义的属性“ map”(ReactJS和AJAX) - Cannot read property 'map' of undefined ( ReactJS & AJAX)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM