gpt4 book ai didi

javascript - JS/无法读取未定义的属性 'push'

转载 作者:行者123 更新时间:2023-11-28 15:30:05 24 4
gpt4 key购买 nike

我在 JS 中有以下代码:

if (all[j][i][11] != Boolean(1) && $scope.w[j][i] != undefined && $scope.ip[j][i] != undefined && $scope.type[j][i] != undefined)
var specificArray = []
specificArray.push($scope.w[j][i]);
// continue with the code

它给了我下一个错误:

TypeError: Cannot read property 'push' of undefined

但是 SpecificArray 是在推送操作之前定义的。

我也尝试:var SpecificArray = new Array()

最佳答案

您应该在条件上添加一些花括号

if (all[j][i][11] != Boolean(1) && $scope.w[j][i] != undefined && $scope.ip[j][i] != undefined && $scope.type[j][i] != undefined){
var specificArray = []
specificArray.push($scope.w[j][i]);
}

没有它,行为是这样的:

if (all[j][i][11] != Boolean(1) && $scope.w[j][i] != undefined && $scope.ip[j][i] != undefined && $scope.type[j][i] != undefined){
var specificArray = [];
}
specificArray.push($scope.w[j][i]); // Cannot read property 'push' of undefined

关于javascript - JS/无法读取未定义的属性 'push',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27719843/

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