如何遍历嵌套对象和数组中的 return 值?
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]
映射为新对象的 key 时,您将日期设置为键,而不是 "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);
我目前正在尝试从 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]
映射为新对象的 key 时,您将日期设置为键,而不是 "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);