[英]Unable to store the state of an array inside map - Reactjs
I am working on reactjs project where i have to store the array value in a local variable that i am parsing form a JSON data . 我在Reactjs项目上工作,我必须将数组值存储在我要从JSON数据解析的局部变量中。 I am successfully able to parse the data using map function , i want to store the result data in a global array , but i am unable to declare state inside map function, how do can i get access to the array which i am retrieving through map. 我可以使用map函数成功解析数据,我想将结果数据存储在一个全局数组中,但是我无法在map函数中声明状态,我该如何访问通过map检索的数组。 below is my code. 下面是我的代码。
//post data input
let postData = { Userid: this.props.Userid };
//post data is a method which return's the json array its a post request
PostData('UserDetails', postData).then((result) => {
//storing the data in a variable
responseJson = result;
{
//parsing the json using map
responseJson.Jiralist.map((rowdata, i) => (
// i want to store this value in a global array
console.log("", rowdata.jirakey);
))
}
The map() method creates a new array with the results of calling a provided function on every element in the calling array. map()方法创建一个新数组,并在调用数组中的每个元素上调用提供的函数。 If dont really need to modify the existing array, you can use forEach. 如果确实不需要修改现有阵列,则可以使用forEach。 And create a global array and push the new item using spread operator 并创建一个全局数组,并使用传播运算符推送新项
let globalArr = [];
responseJson.Jiralist.forEach((rowdata, i) => ([...globalArr, rowdata.jirakey]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.