我不断获取(无法读取未定义的属性'Date')

问题描述 投票:0回答:1

我的目标是在数据数组中向后移动,检查只要日期/月份(例如'02')保持不变,我只会采用第一种情况(例如data.Cases:1234)月。

这里,我说(i)以保存数组的最大长度。casesdate携带最大案例数和位置i的月份值。

    let data = 'https://api.covid19api.com/total/country/south-africa/status/confirmed?from=2020-03-01T00:00:00Z&to=2020-04-01T00:00:00Z';

    let i = (data.length -1);
    let cases = data[i].Cases; // Total sum
    let date = (data[i].Date).slice(6,7);
    let getCase = [];
    let getDate = [];
    let theMonths = [];

    do {
        // Check if the date matches first date.
        // Push that date to getDate array.
        // Push that case to getCase array.
        // Continue loop

        if (date === (data[i].Date).slice(6,7)) {
            i--;
        }

然后,当月份值更改时,此语句应触发,并将循环最初使用的日期变量更改为触发该语句的新变量。因此,我可以将新值推送到数组中,并使用下一个日期值重复整个循环。

            // If the date has changed
        if (date !== (data[i].Date).slice(6,7)) {
            // Change to current Date

            //ERROR HERE (Does not update date with new date)

            if (data[i].Date){
                date = (data[i].Date).slice(6,7);
                getDate.push(date);
                getCase.push(data[i].Cases);
            }
        }

        if (date === '1') {
            break;
        }

    }

    while (
        cases >= data[i].Cases
    );
javascript reactjs undefined do-while
1个回答
0
投票
getCasesPerMonth(data) {

    // Loop backwards through array, storing the first date in temp variable and case amount in array, compare next 
    // iteration date with prev variable, if true replace var with current date and proceed loop.
    // if false, push new case to array and prev date variable to array and procceed loop.

    let i = data.length -1;

    // This needs to be updated when the date changes
    let currentDate = data[i].Date.slice(6,7)

    let addDates = [currentDate];
    let addCases = [data[i].Cases];

    while(i > 0) {

        if ((data[i].Date).slice(6,7) !== currentDate) {

            addDates.push((data[i].Date).slice(6,7));
            addCases.push(data[i].Cases);

            currentDate = (data[i].Date).slice(6,7);
        }

        i--;

    }

    let orderedDates = [];
    let orderedCases = [];

    this.flipAndPush(addDates, orderedDates);
    this.flipAndPush(addCases, orderedCases);

    let writtenMonths = [];

    orderedDates.forEach((date) => {
        writtenMonths.push(this.findMonth(date));
    });

    this.setState({splitMonths: writtenMonths, splitCases: orderedCases});
}
© www.soinside.com 2019 - 2024. All rights reserved.