我试图在反应中的对象数组上执行地图,但收到错误
TypeError: this.props.historyAtLocation.map is not a function. (In 'this.props.historyAtLocation.map(this.renderWeather)', 'this.props.historyAtLocation.map' is undefined)
但是,如果我提出console.log('test_output: ', this.props.historyAtLocation)
结果如下:
test_output:
Array (24)
{time: 1476469800, summary: "Partly Cloudy", icon: "partly-cloudy-night", precipIntensity: 0, precipProbability: 0, …}
{time: 1476473400, summary: "Clear", icon: "clear-night", precipIntensity: 0, precipProbability: 0, …}
{time: 1476477000, summary: "Clear", icon: "clear-night", precipIntensity: 0, precipProbability: 0, …}
...
所以你可以看到它被定义了,它是一个对象数组。
我正在尝试这样map
:
this.props.historyAtLocation.map(this.renderWeather)
我知道这个问题已经提出了很多次,但没有一个解决方案适合我。
以防它可能有用,这是我的相关减速器
function historyAtLocation(state = { }, action) {
switch (action.type) {
case RECEIVED_FORECAST:
{console.log('sample data: ', action.forecast.hourly.data)}
return action.forecast.hourly.data
default:
return state
}
}
我认为数组中的对象可能存在一些问题吗?
感谢任何想法,谢谢
UPD 27.10.16 如果我跑
const hal = this.props.historyAtLocation;
console.log(Array.isArray(hal), hal, typeof hal, hal.map, typeof hal.map)
正如@biphobe建议的那样,我收到以下输出: