简体   繁体   English

Javascript:定义了2d数组,但是当将2d数组的元素赋值给变量时,它变为'undefined'

[英]Javascript: 2d array is defined, but when assigning an element of the 2d array to a variable, it becomes 'undefined'

I'm creating a Google Chrome extension and working with Javascript. 我正在创建Google Chrome扩展程序并使用Javascript。 When printing an element of a 2d array, it works perfectly fine. 打印二维数组的元素时,它可以正常工作。 However, when I set that element to a variable. 但是,当我将该元素设置为变量时。 It gives this error, 它给出了这个错误,

Error handling response: TypeError: Cannot read property '3' of undefined 错误处理响应:TypeError:无法读取未定义的属性“3”

I've set up debugging statements that clearly show that the 2d array is not undefined. 我已经设置了调试语句,清楚地表明2d数组没有未定义。 But assigning an element to a variable makes it undefined somehow. 但是将一个元素赋给变量会使它以某种方式未定义。

previous_data = [
    ["AP Micro Economics ", 95.8, 94.1, 95.2, 95.4],
    ["IB Literature_Language I HL ", 95.9, 93.6, 98.4, 95.4],
    ["AP Calculus AB ", 97.5, 93.9, 98.5, 96.6],
    ["Adv Topics Comp Science ", 98.3, 96.3, 93, 95.7],
    ["Physics ", 95, 99.1, 94.9, 96.3],
    ["Simulations and Num Models ", "N", 95, "N", "N"],
    ["IB Hist of Amer I HL ", 94.2, 93.9, 92.2, 93.4],
    ["IB Espanol IV SL ", 96.1, 95.4, 92.1, 94.5],
    ["Structured Query Lang ", "N", 98.6, "N", "N"],
    ["Artificial Intelligence ", "N", "N", 91.8, "N"]
];
for (subject = 0; subject < 1; subject++){
    for (section = 1; section < current_data[subject].length; section++){
        if (previous_data[subject][section] != current_data[subject][section]){
            // These print properly
            console.log("previous_data");
            console.log(previous_data);
            //This prints properly
            var subject = previous_data[subject][0];
            console.log(subject)
            // This is where the code breaks
            var before = previous_data[section];
            console.log(previous_data[subject][section])
        }
    }
}

Any idea why previous_data is being read as undefined? 知道为什么previous_data被读取为undefined?

You are using subject as a variable twice. 您将主题用作变量两次。 You have to differentiate between the variable value and the variable index. 您必须区分变量值和变量索引。

for (subjectIndex = 0; subjectIndex < 1; subjectIndex++){
    for (sectionIndex = 1; sectionIndex < current_data[subjectIndex].length; sectionIndex++){
        if (previous_data[subjectIndex][sectionIndex] != current_data[subjectIndex][sectionIndex]){
            // These print properly
            console.log("previous_data");
            console.log(previous_data);
            //This prints properly
            var subject = previous_data[subjectIndex][0];
            console.log(subject + " at index [" + subjectIndex + "][0]");
            // This is where the code breaks
            var before = previous_class[sectionIndex];
            console.log(previous_data[subjectIndex][sectionIndex])
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM