API 不使用变量进行调用。 React.js,时刻-工作日
我已经被这个问题困扰了一段时间了,所以我想是时候问问我那些很酷的全球天才了。
我想做的事情:创建一个数组数组,自动将日期和数据配对(通过循环)。复杂的因素包括尝试仅获取工作日的日期并排除节假日。
问题:当我使用变量时,API 数据不显示。此特定行是问题 arrayOfDateAndPrice.push(data[2].data['Time Series (Daily)'][today]['4.close'])
'data' 保存 API我正在取。我正在挖掘 data[2].data['Time Series'] 等中的 JSON。当我插入带有变量的日期时(即我从循环中获得的日期,我检查过,它会以字符串格式获取正确的日期) 。它不起作用。但是,当我插入字符串正常日期(即“2022-03-04”)时,它会显示数据!?我到底错过了什么?
错误:类型错误:未定义不是对象(评估'data[2].data['时间序列(每日)'][today]['4.close']')
const calculateBusinessDay = (data) => {
const presDay = '02-21-2022';
const goodFri = '04-15-2022';
const memDay = '05-30-2022';
const junTeenth = '06-20-2022';
const july4th = '07-04-2022';
const laborDay = '09-05-2022';
const thanksGiving = '11-24-2022';
const xMas = '12-26-2022';
var today;
var arrayOfDateAndPrice = [];
const arrayOfAllData = [];
const weekdays = momentBusiness
weekdays.updateLocale('us', {
holidays: [mlk, presDay, goodFri, memDay, junTeenth, july4th, laborDay, thanksGiving, xMas],
holidayFormat: 'MM-DD-YYYY',
workingWeekdays: [1, 2, 3, 4, 5]
});
for (let i = 0; i < 30; i++) {
today = weekdays().subtract(i, 'days').format('YYYY-MM-DD')
if (weekdays(today).isBusinessDay() && !weekdays(today).isHoliday() == true) {
arrayOfDateAndPrice.push(today)
arrayOfDateAndPrice.push(data[2].data['Time Series (Daily)'][today]['4. close'])
arrayOfAllData.push(arrayOfDateAndPrice)
arrayOfDateAndPrice = [];
}
}
return (
arrayOfAllData
)
}
I have been stuck on this problem for a while now, so I think it is time to ask my cool worldwide geniuses.
What I am trying to do: Create An array of arrays that pairs a date and data automatically (through a loop). Complicating factors including trying to get dates of only weekdays and exclude holidays.
Problem: When I use a variable, the API data doesn't display. This particular line is the problem arrayOfDateAndPrice.push(data[2].data['Time Series (Daily)'][today]['4. close'])
the 'data' holds the API that I am fetching. I am digging through the JSON in data[2].data['Time Series'] etc. When I insert the date with a variable (i.e date I got from the loop, I checked, it fetches the correct date in string format). it doesn't work. BUT, when I insert a stringed normal date (i.e. '2022-03-04') it displays the data!? What the heck am I missing?
Error: TypeError: undefined is not an object (evaluating 'data[2].data['Time Series (Daily)'][today]['4. close']')
const calculateBusinessDay = (data) => {
const presDay = '02-21-2022';
const goodFri = '04-15-2022';
const memDay = '05-30-2022';
const junTeenth = '06-20-2022';
const july4th = '07-04-2022';
const laborDay = '09-05-2022';
const thanksGiving = '11-24-2022';
const xMas = '12-26-2022';
var today;
var arrayOfDateAndPrice = [];
const arrayOfAllData = [];
const weekdays = momentBusiness
weekdays.updateLocale('us', {
holidays: [mlk, presDay, goodFri, memDay, junTeenth, july4th, laborDay, thanksGiving, xMas],
holidayFormat: 'MM-DD-YYYY',
workingWeekdays: [1, 2, 3, 4, 5]
});
for (let i = 0; i < 30; i++) {
today = weekdays().subtract(i, 'days').format('YYYY-MM-DD')
if (weekdays(today).isBusinessDay() && !weekdays(today).isHoliday() == true) {
arrayOfDateAndPrice.push(today)
arrayOfDateAndPrice.push(data[2].data['Time Series (Daily)'][today]['4. close'])
arrayOfAllData.push(arrayOfDateAndPrice)
arrayOfDateAndPrice = [];
}
}
return (
arrayOfAllData
)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了?!?!这是因为我的“i”变量从“今天”(日本时间)开始。日本时间没有市场数据,因为我们在时间上“领先”,哈哈。哦天哪。感觉很好...
“i”必须从 i=1 开始,从今天减去一天并开始循环。
I figured it out?!?! It was because my 'i' variable started the day from "today" which was Japan time. No market data for Japan time since we are "ahead" in time lol. Oh gosh. That feels good...
The 'i' had to start at i=1, to subtract one day from today and start the loop.