I'm currently working on extracting data points from an API response to use in graphing. My goal is to extract an array of the "4. close" values from the object below.
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"
}
}
}
// Desired output => [1846, 1880, 1830]
Here's my current code:
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"])
}
}
I attempted to map the nested object into an array of objects, thinking it would help me iterate through the data correctly, but I seem to be facing issues as I am getting undefined values. Any assistance would be greatly appreciated!