I want to iterate through an array using for loop instead of using foreach in which i m not able to break the loop. But here in for loop i getting $scope.items[i] as undefined! here i m dynamically adding values in the array based upon the condition.
$scope.items= [{
name: 'abhishek',
credit: 1233,
debit: 0,
balance: 12
}];
for (var i = 1; i <= $scope.items.length; i++) {
console.log($scope.items.length);
if ($scope.selectedName.value === $scope.items[i].name) {
console.log($scope.items[i].name + "forif" + true);
break;
}
else {
console.log($scope.items[i].name + "forelse " + false);
break;
}
}
$scope.itemsor$scope.ledger? Because$scope.ledgeris not defined in your piece of code< $scope.items.lengthsince$scope.items[$scope.items.length]would be out of bounds.)i = 0for your for loop, your$scope.itemsonly has 1 object in its array, so there is nothing in the$scope.items[1]and yourconsole.log($scope.items[i].namewill be undefined because again - there is nothing at index 1