gpt4 book ai didi

javascript - 数组数组中未定义的属性

转载 作者:行者123 更新时间:2023-12-02 16:08:02 24 4
gpt4 key购买 nike

我正在处理数组的数组,我想测试每个第一个值,但我得到的只是未捕获的类型错误:无法读取未定义的属性“0”。请帮忙!

 function getManualDesactivation(data){

var tab=[];
var l=data.length ;
var listeService= getCategorie(data);
var resultat=[];

for(var i=0; i<l;i++){
if (data[i][1] == "DESACTIVATION") {
var subtab=[];
subtab.push(data[i][0]);
subtab.push(data[i][2]);
tab.push(subtab);


}
}

if (tab.length > 1) {
var j = 0;
for (var i = 0; i < listeService.length; i++) {

if (listeService[i] == tab[j][0]) {<---- here is the exception
resultat.push(tab[j][1]);

j++;
} else {

resultat.push(0);
j++;
}

}
}

最佳答案

该错误是由于尝试访问不存在的变量的 0 索引引起的。

为了确保您正在检查定义值的索引,您可以编写:

    if (tab[j] && listeService[i] == tab[j][0]) {
resultat.push(tab[j][1]);

j++;
} else {

resultat.push(0);
j++;
}

关于javascript - 数组数组中未定义的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30528233/

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