One major issue you seem to be facing is the assumption that you're dealing with an Array when in fact, it's not.
The contents of dailyLong
actually represent an object. The confusion arises due to the need to access keys within dailyLong
using bracket notation []
, given the presence of spaces (Time Series (Daily)
) and special characters (2021-04-12
). For more information on property accessors, you can refer here.
Based on your code, it seems like you have an array named dateMin
containing the dates you wish to search through, which makes it the target for looping through to obtain daily closing prices (making it an actual array).
The purpose behind your second line isn't entirely clear; however, if your goal is simply to iterate over all the closing prices, then eliminate the +
signs from the assignment (refer to the code snippet below).
Your first line is accurate because it accesses the object using bracket notation rather than dot notation. Just swap out the 1
with i
. On the other hand, the second line is incorrect based on what you're attempting to achieve:
const dailyLong = {
"Time Series (Daily)": {
"2021-04-12": {
"4. close": 123
}
}
}
// Correct, works:
dailyLong['Time Series (Daily)'][dateMin[1]]['4. close']
// Not incorrect, but not what you're trying to do:
dailyLong['Time Series (Daily)'] + [dateMin[i]] + ['4. close']
// First you get a reference to the object
const object = dailyLong['Time Series (Daily)'] // [object Object]
// Second you create a new array with 1 element, dailyMin[i] or '2021-04-12'
const array1 = [dailyMin[i]] // 2021-03-12
// Finally you create a second array with one element, '4. close'
const array2 = ['4. close'] // 4. close
object + array1 + array2 // [object Object]2021-03-124. close
Below is a code snippet demonstrating how you can traverse these values:
const dailyLong = {
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "IBM",
"3. Last Refreshed": "2021-04-12",
"4. Output Size": "Full size",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2021-04-12": {
"1. open": "135.0200",
"2. high": "135.3700",
"3. low": "133.8500",
"4. close": "134.5900",
"5. volume": "3753959"
},
"2021-04-09": {
"1. open": "134.8700",
"2. high": "135.7400",
"3. low": "134.7100",
"4. close": "135.7300",
"5. volume": "3023916"
},
"2021-04-08": {
"1. open": "134.5700",
"2. high": "135.6299",
"3. low": "134.1600",
"4. close": "135.1200",
"5. volume": "4087228"
},
"2021-04-07": {
"1. open": "133.8400",
"2. high": "134.9400",
"3. low": "133.7800",
"4. close": "134.9300",
"5. volume": "2976136"
},
"2021-04-06": {
"1. open": "135.5800",
"2. high": "135.6400",
"3. low": "134.0900",
"4. close": "134.2200",
"5. volume": "3620964"
},
}
}
const dateMin = [
'2021-04-12',
'2021-04-09',
'2021-04-08',
'2021-04-07',
'2021-04-06',
]
function kursMinus() {
for (var i = 0; i < 5; i++) {
console.log(dailyLong['Time Series (Daily)'][dateMin[1]]['4. close']); //works
console.log(dailyLong['Time Series (Daily)'][dateMin[i]]['4. close']); //works
// kursMin[i] = dailyLong['Time Series (Daily)'] + [dateMin[i]] + ['4. close']; //doesn't work
}
}
kursMinus()