Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.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
在Javascript中选择数组时遇到问题_Javascript_Selector - Fatal编程技术网

在Javascript中选择数组时遇到问题

在Javascript中选择数组时遇到问题,javascript,selector,Javascript,Selector,Javascript的新特性。试图从一个项目的天气API中提取数据,但遇到了一个我相信很容易解决的问题。以下是我已经获取的数据: {coord: {…}, weather: Array(1), base: "stations", main: {…}, visibility: 9656, …} coord: {lon: -76.13, lat: 43.04} weather: Array(1) 0: {id: 500, main: "Rain", description: "light

Javascript的新特性。试图从一个项目的天气API中提取数据,但遇到了一个我相信很容易解决的问题。以下是我已经获取的数据:

{coord: {…}, weather: Array(1), base: "stations", main: {…}, visibility: 9656, …}
coord: {lon: -76.13, lat: 43.04}
weather: Array(1)
     0: {id: 500, main: "Rain", description: "light rain", icon: "10d"}
     length: 1
__proto__: Array(0)
base: "stations"
main: {temp: 281.12, feels_like: 274.24, temp_min: 280.37, temp_max: 282.04, pressure: 1004, …}
visibility: 9656
wind: {speed: 8.2, deg: 310}
rain: {1h: 0.25}
clouds: {all: 90}
dt: 1587324361
sys: {type: 1, id: 5965, country: "US", sunrise: 1587291309, sunset: 1587340289}
timezone: -14400
id: 0
name: "Syracuse"
cod: 200
__proto__: Object
我需要做的是在天气课下选择“雨”。但是,由于它位于数组中,我不知道如何获取它。例如,如果我

data.visibility

我当然会带着9656回来。但如果我这样做了

data.weather.0
甚至
data.weather。[“0”]
我将遇到以下错误:未捕获的语法错误:意外数字。即使我没有得到这个错误,我如何访问数组中的特定元素“Rain”


很抱歉,如果这是一个简单的解决方案,那么在我搜索时,由于非常具体的措辞,很难找到答案。

您可以这样访问它:

data.weather[0].main


其中0是一个描述数组元素索引的整数。

您可以这样访问它:

data.weather[0].main


其中0是描述数组元素索引的整数。

如果只需要数组的第一个元素,只需使用索引即可获取值
data.weather[0]。main

或者可以通过数组方法
data.weather.map(item=>item.main)
映射数组。您将获得一个自定义数组[“Rain”]

如果您只需要数组的第一个元素,只需使用索引即可获得值
data.weather[0].main
。 或者可以通过数组方法
data.weather.map(item=>item.main)
映射数组。您将获得一个自定义数组[“Rain”]