React 数组中对象的 .map

.map of an object in an array in React

我试图遍历一个包含数组的对象,该数组在 React 中包含对象内部的对象,但我在 .map 函数上遇到错误。这是 .map 函数,后面是对象在 this.state

中的样子

.地图:

//trying to access January from the object which is 0 starting month
let daysOfWeeks = this.state[0].map(function (day, i){
    ...
}

这里是一个对象的例子:

//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

我在 .map 中想要的是让 day 成为我循环遍历的对象。 day.dayNumberday.dayOfWeek

... .map(function(day, i){
    return(
        <div>
            <div>{day.dayNumber}</div>
            <div>{day.dayOfWeek}</div>
        </div>
    )
}

但是 .map 上的错误是 this.state[0] 我认为是问题所在。任何人都知道我如何访问和循环访问父对象内数组中的对象?

尝试this.state.result['0'].map(...)

当你有一个像这样的对象时,以数字作为键,即使键是“数字”;您将其作为字符串访问。默认情况下,对象键是字符串。

let result = {
    0:[ 
        {dayNumber:1,dayOfWeek:"fri"},
        {dayNumber:2,dayOfWeek:"sat"},
        {dayNumber:3,dayOfWeek:"sun"},
        ...
      ]
}