Ad
“.push Is Not A Function” Error While Inserting Key-value To An Array In AngularJS
I’m trying push ItemId(as a key)
and PartNO(value)
to an array dynamically based on Noof ItemPartDtls
using a for
loop so that I can again fetch those PartNO
s using ItemID
in the for
loop when required.
But I got the error message:
TypeError: NoofParts.push is not a function
while pushing.
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);
}
}
}
Ad
Answer
Try like this way
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);
}
}
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad