我目前正在尝试从 API 响应中获取数据点以用于绘图。我有兴趣返回以下对象的“4.close”值数组。
let res = {
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "amzn",
"3. Last Refreshed": "2020-03-20",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2020-03-20": {
"1. open": "1926.3100",
"2. high": "1957.0000",
"3. low": "1820.7300",
"4. close": "1846.0900",
"5. volume": "9740990"
},
"2020-03-19": {
"1. open": "1860.0000",
"2. high": "1945.0000",
"3. low": "1832.6500",
"4. close": "1880.9300",
"5. volume": "10399943"
},
"2020-03-18": {
"1. open": "1750.0000",
"2. high": "1841.6600",
"3. low": "1745.0000",
"4. close": "1830.0000",
"5. volume": "9596297"
}
}
}
// I need this returned => [1846, 1880, 1830]
目前我的代码如下所示:
const parsed = res["Time Series (Daily)"]
const datesArr = Object.entries(parsed).map((e) => ( { [e[0]]: e[1] } ))
function getYCoords() {
for(i=0;i<datesArr.length;i++) {
let dateObj = datesArr[i]
console.log(dateObj["4. close"])
}
}
我使用 map 将嵌套对象转换为对象数组,希望这能帮助我正确地遍历数据,但我认为我让事情变得更加困难并且在这一点上变得不确定。谁能帮我吗?