I am using Alpha Vantage to update stocks on a website that I am making and I can't get the deconstruction to work properly with the date variable. (It works correctly when I place a static date so I know that this is the problem.)
var today = new Date();
var date = today.getFullYear()+'-'+ pad2(today.getMonth()+1) +'-'+pad2(today.getDate());
function pad2(number) {
return (number < 10 ? '0' : '') + number;
}
const alpha = alphavantage({ key: 'removed' });
alpha.data.daily(`msft`).then(data => {
let {
'Meta Data': {
'1. Information':information,
'2. Symbol':symbol,
'3. Last Refreshed':lastrefreshed,
'4. Output Size':outputsize,
'5. Time Zone':timezone
},
'Time Series (Daily)': {
date: { //breaks here
'1. open':open,
'2. high':high,
'3. low':low,
'4. close':close,
'5. volume':volume,
}
}
} = data;
document.getElementById('stock').innerHTML = open;
});
//This is the part of the object that I need to deconstruct, the date changes every day
"Time Series (Daily)": {
"2020-04-02": { // Where I can't get it to work
"1. open": "105.3700",
"2. high": "110.3200",
"3. low": "105.1400",
"4. close": "110.0000",
"5. volume": "6273128"
},
"2020-04-01": {
"1. open": "106.3600",
"2. high": "109.9200",
"3. low": "104.5210",
"4. close": "105.1400",
"5. volume": "6111890"
}, //and so on
}
let data = {
//This is the part of the object that I need to deconstruct, the date changes every day
'Time Series (Daily)': {
'2020-04-02': {
// Where I can't get it to work
'1. open': '105.3700',
'2. high': '110.3200',
'3. low': '105.1400',
'4. close': '110.0000',
'5. volume': '6273128',
},
'2020-04-01': {
'1. open': '106.3600',
'2. high': '109.9200',
'3. low': '104.5210',
'4. close': '105.1400',
'5. volume': '6111890',
}, //and so on
},
};
var date = '2020-04-02';
let {
'Time Series (Daily)': {
[date]: {
//breaks here
'1. open': open,
'2. high': high,
'3. low': low,
'4. close': close,
'5. volume': volume,
},
},
} = data;
console.log({ open, high, low, close, volume });