简体   繁体   中英

How do I Iterate through a nested object and return values in an array?

I am currently trying to get data points from an API response for use in graphing. I am interested in returning an array of the "4. close" values of the following object.


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]

Currently my code looks like this:

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 used map to turn the nested object into an array of object, hoping that would help me iterate through the data correctly, but I think i've made things more difficult and am getting undefined at this point. Can anyone help me out?

Your issue is that Object.entries(parsed) gives you an array which looks like:

[ "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" } ] ]

... so when you map e[0] to be the key of a new object, you're setting the date as the key, not the "4. close" etc. properties from your objects. An easy fix would be to map the entries to the object stored at e[1] which contains all the numbered properties:

 const 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" } } } const parsed = res["Time Series (Daily)"]; function getYCoords() { for (i = 0; i < datesArr.length; i++) { let dateObj = datesArr[i] console.log(dateObj["4. close"]) } } const datesArr = Object.entries(parsed).map((e) => e[1]); getYCoords();

However, as you are only concerned about the values, there is no need to get the entries (which contains both the keys and the values). Instead, you can get the Object.values() (ie an array of your objects) and then map those to values stored at "4. close" :

 const 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" } } } const parsed = res["Time Series (Daily)"]; const datesArr = Object.values(parsed).map((e) => +e["4. close"]); // + to turn string into number console.log(datesArr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM