我正在尝试使用ItemId(as a key)
循环基于PartNO(value)
将Noof ItemPartDtls
和for
动态推入数组,以便再次获取那些PartNO
必要时在ItemID
循环中使用for
。
但是我收到了错误消息:
TypeError: NoofParts.push is not a function
同时按下。
var NoofParts = []; // initialized globally
if ($scope.ItemPartDtls.length > 0) {
for (let e = 0; e <= $scope.ItemPartDtls.length - 1; e++) {
// NoofParts.push({ "ItemID": $scope.ItemsInfo[d].ITEM_ID, "PartNO": $scope.ItemPartDtls[e].PART_NO });
NoofParts.push({ ItemID: $scope.ItemsInfo[d].ITEM_ID, PartNO: $scope.ItemPartDtls[e].PART_NO});
// error here: TypeError: NoofParts.push is not a function
}
}
...
function GetPartdtls(ItmId){
for(i = 0; i < NoofParts.length; i++){
if(NoofParts[i].ItemID == ItmId) {
console.log("ItemID:- " + NoofParts[i].ItemID + " PartNO:- " + NoofParts[i].PartNO);
}
}
}
答案 0 :(得分:1)
以这种方式尝试
var NoofParts = []; // initialized globally
if ($scope.ItemPartDtls.length > 0) {
for (let e = 0; e <= $scope.ItemPartDtls.length - 1; e++) {
// NoofParts.push({ "ItemID": $scope.ItemsInfo [d].ITEM_ID, "PartNO": $scope.ItemPartDtls[e].PART_NO});
let newArr = {
ItemID: $scope.ItemsInfo[e].ITEM_ID, //here your typo error
PartNO: $scope.ItemPartDtls[e].PART_NO
}
NoofParts.push(newArr);
}
}
答案 1 :(得分:0)
理想情况下,您的代码应该可以工作,但是要再次证明,请尝试以下方法
(function() {
let NoofParts = []; // initialized globally
if ($scope.ItemPartDtls.length > 0) {
for (let e = 0; e <= $scope.ItemPartDtls.length - 1; e++) {
if (!Array.isArray(NoofParts)) {
NoofParts = [];
}
NoofParts.push({
ItemID: $scope.ItemsInfo[d].ITEM_ID,
PartNO: $scope.ItemPartDtls[e].PART_NO
}); // error here: TypeError: NoofParts.push is not a function
}
}
})();