[英].map of an object in an array in React
I am trying to loop through an object that contains arrays which contain objects inside of them in React and I get an error on the .map
function. 我试图遍历一个包含数组的对象,这些数组在React中包含其中的对象,并且
.map
函数出现错误。 Here is the .map
function followed by what the object looks like in this.state
这是
.map
函数,其后跟对象在this.state
中的this.state
.map: 。地图:
//trying to access January from the object which is 0 starting month
let daysOfWeeks = this.state[0].map(function (day, i){
...
}
Here is an example of the object: 这是对象的示例:
//0 is January, 1 is February, and so on... inside has objects that have the day number and day of week
let result = {
0:[
{dayNumber:1,dayOfWeek:"fri"},
{dayNumber:2,dayOfWeek:"sat"},
{dayNumber:3,dayOfWeek:"sun"},
...
],
1:[
{dayNumber:1,dayOfWeek:"mon"},
{dayNumber:2,dayOfWeek:"tue"},
{dayNumber:3,dayOfWeek:"wed"},
...
],
2:[
{dayNumber:1,dayOfWeek:"tue"},
{dayNumber:2,dayOfWeek:"wed"},
{dayNumber:3,dayOfWeek:"thur"},
...
],
...
};
//result is in a function that is returned and set state is calling the function and setting state to the object itself -- here is setState:
this.setState(getDaysArray(y,m));
//getDaysArray(y,m) returns that object above
What I want in the .map
is for day to be the object I loop through. 我想要的
.map
今天是我循环通过的对象。 day.dayNumber
and day.dayOfWeek
day.dayNumber
和day.dayOfWeek
... .map(function(day, i){
return(
<div>
<div>{day.dayNumber}</div>
<div>{day.dayOfWeek}</div>
</div>
)
}
But what errors in on the .map
is this.state[0]
I think is the issue. 但是,我认为问题在于
.map
上的错误是this.state[0]
。 Anyone know how I can access and loop through the objects in the array inside the parent object? 有谁知道我如何访问和遍历父对象内部数组中的对象?
Try this.state.result['0'].map(...)
试试
this.state.result['0'].map(...)
When you have a an object like this, with a number as the key, even though the key is a 'number; 当您有一个像这样的对象时,即使数字是“数字”,也要用数字作为数字。 you access it as a string.
您以字符串形式访问它。 Object keys by default are strings.
默认情况下,对象键是字符串。
let result = {
0:[
{dayNumber:1,dayOfWeek:"fri"},
{dayNumber:2,dayOfWeek:"sat"},
{dayNumber:3,dayOfWeek:"sun"},
...
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.