繁体   English   中英

如何遍历嵌套对象并返回数组中的值?

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

我目前正在尝试从 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 将嵌套对象转换为对象数组,希望这能帮助我正确地遍历数据,但我认为我让事情变得更加困难并且此时变得未定义。 谁能帮我吗?

您的问题是Object.entries(parsed)为您提供了一个如下所示的数组:

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

...因此,当您将e[0]映射为新对象的时,您将日期设置为键,而不是对象中的"4. close"等属性。 一个简单的解决方法是将条目映射到存储在e[1]的对象,该对象包含所有编号的属性:

 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();

但是,由于您只关心值,因此无需获取条目(其中包含键和值)。 相反,您可以获取Object.values() (即您的对象数组),然后将它们映射到存储在"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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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