You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从返回两个数组的API中提取单个temperature数组数据?

提取API返回的温度数组解决方案

直接通过对象属性链式访问就能拿到单独的temperature_2m数组,步骤如下:

  1. 假设API返回的数据存储在变量(比如weatherData)中,直接访问weatherData.hourly.temperature_2m即可得到目标数组。

示例代码:

// 假设这是API返回的完整数据
const weatherData = {
  latitude: 51.75,
  longitude: 19.5,
  generationtime_ms: 0.28395652770996094,
  utc_offset_seconds: 0,
  timezone: 'GMT',
  timezone_abbreviation: 'GMT',
  elevation: 218,
  hourly_units: { time: 'iso8601', temperature_2m: '°C' },
  hourly: {
    time: ['2022-11-26T00:00', '2022-11-26T01:00', /* ... */],
    temperature_2m: [2.4, 2.3, 2.1, /* ... */, -1]
  }
};

// 提取单独的温度数组
const temperatureArray = weatherData.hourly.temperature_2m;
console.log(temperatureArray); // 输出仅包含温度数值的数组

错误原因排查

你之前把所有数据都放进新数组,大概率是因为没有精准访问到temperature_2m属性:

  • 比如误把整个hourly对象赋值(const tempArray = weatherData.hourly),这样会包含time和temperature_2m两个数组;
  • 或者直接赋值了整个API返回对象(const tempArray = weatherData),自然会包含所有顶层属性。

只要明确层级关系,直接访问到最内层的temperature_2m就能拿到单独的温度数组。

内容的提问来源于stack exchange,提问作者dzikilosos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 23:15:43