gpt4 book ai didi

javascript - (Javascript) 解构对象但对象有更改日期

转载 作者:行者123 更新时间:2023-12-02 20:55:25 25 4
gpt4 key购买 nike

我正在使用 Alpha Vantage 更新我正在制作的网站上的股票,但我无法使解构与日期变量一起正常工作。 (当我放置静态日期时它可以正常工作,所以我知道这就是问题所在。)

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
}

最佳答案

使用computed property names :

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

关于javascript - (Javascript) 解构对象但对象有更改日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61005036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com