Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在React Native=can';是否不引用数组项?_Arrays_Object_React Native Android - Fatal编程技术网

Arrays 在React Native=can';是否不引用数组项?

Arrays 在React Native=can';是否不引用数组项?,arrays,object,react-native-android,Arrays,Object,React Native Android,我正在使用React Native和Open Weather Map API构建一个小型天气应用程序。我从API调用中检索到的数据超出了我想要使用的范围,因此我只解析出我需要的片段,并将它们存储在一个数组中。然后我获取数组并将其设置为状态对象。我可以引用这个对象,但它不是说我的数组是一个数组,而是说它是一个对象,因此不允许我对它使用任何数组方法。我该怎么做 //reponseData is the data retrieved from the API call; the data retrie

我正在使用React Native和Open Weather Map API构建一个小型天气应用程序。我从API调用中检索到的数据超出了我想要使用的范围,因此我只解析出我需要的片段,并将它们存储在一个数组中。然后我获取数组并将其设置为状态对象。我可以引用这个对象,但它不是说我的数组是一个数组,而是说它是一个对象,因此不允许我对它使用任何数组方法。我该怎么做

//reponseData is the data retrieved from the API call; the data retrieved is an object with arrays and objects 
within.  The forecast data for the next five days is given in 3 hour increments, so you have a 40 item array of 
data pieces.  I loop through this list of 40 items, pull out just what I need...

let forecastArray = [];

for (let i=0; i<responseData.list.length; i++) {
    let day = responseData.list[i].date
    let high = responseData.list[i].weather[0].hiTemp
    let low = responseData.list[i].weather[0].loTemp
    let condition = responseData.list[i].sys.condition

    forecastArray.push(day)
    forecastArray.push(high)
    forecastArray.push(low)
    forecastArray.push(condition)

    this.setState({
        forecastData: forecastArray
    })
例如,引用this.state.forecastData[2]会给我带来错误。所以我检查了this.state.forecast的类型以了解原因,它说数组是一个对象?我需要进一步划分数组数据并对其进行操作。前几个项目(e.x.forecastData[0]到forecastData[9]将用于2019年6月11日下午3点、6点、9点的预测天气,因此我需要提取这些项目,获取最高点和最低点,等等。我无法这样做,因为我甚至无法引用数组中的项目

我尝试过的事情:
使用Object.entries和Object.assign方法,但这只是将项目拆分为几个数组,第一个项目是位置号,第二个项目是数组项目内容。我尝试在使用它的组件中操纵数组,但它仍然是一个对象而不是数组,因此无法引用单个项目。数据集足够大,我不认为最好的做法是将40+项中的每一项都推入它们自己的状态对象键。

在Javascript数组中是对象的一个子集,即它具有相同的原型。因此数组的类型将是一个对象。这不是一个问题。你能用访问
时得到的错误进行更新吗。state.forecastData[2]
因为我认为这与语法无关,而与API调用的持续时间有关

我建议在访问此.state.forecastData[2]时,首先检查其长度是否大于0,这样可以确保数组中存在数据

constructor(props){
forecastData:[]
}
当你使用它时

if(this.state.forecastData.length > 0){
this.state.forecastData[2]
}

试试这个,然后带着疑问回复。

谢谢你,Gaurav Roy!你部分正确。类型一点都不重要。但问题不在于我的API的持续时间,而是我的组件在没有API调用数据的情况下试图呈现!我输入了一个条件

{this.state.forecast !== null && <Forecast forecast=this.state.forecastData />}
{this.state.forecast!==null&&}
现在一切正常。谢谢你的快速回复!

谢谢!:-)我终于一切正常了,请参阅其他帖子了解我所做的。再次感谢!
{this.state.forecast !== null && <Forecast forecast=this.state.forecastData />}