Reputation: 86
I have this array $scope.taxarr
and I am facing some value which is mention below.
$scope.taxarr = [];
for (var co = 0; co < count_item; co++) {
$scope.qty_amt = parseInt($scope.newData1[co].quantity) * parseInt($scope.newData1[co].rate);
$scope.tax_val1 = (parseInt($scope.qty_amt) * parseInt($scope.newData1[co].tax_value)) / 100;
$scope.taxvalue = parseInt($scope.newData1[co].tax_value);
$scope.taxid = parseInt($scope.newData1[co].tax_name);
$scope.loop = $scope.taxarr.length;
if ($scope.loop === 0) {
$scope.taxarr.push({
tax_id: $scope.taxid,
tax_name: $scope.taxvalue,
tax_amount: $scope.tax_val1
});
} else {
for (var i = 0; i < $scope.loop; i++) {
if ($scope.taxid === $scope.taxarr[i].tax_id) {
$scope.taxarr[i].tax_amount = parseInt($scope.taxarr[i].tax_amount) + parseInt($scope.tax_val1);
break;
} else {
$scope.taxarr.push({
tax_id: $scope.taxid,
tax_name: $scope.taxvalue,
tax_amount: $scope.tax_val1
});
}
}
}
console.log($scope.taxarr);
}
I have one array which allows me to check particular id in array object and I face some problem with my inner if ... else
part where I check my id if there match value it is update amount else push object as new record
I am working with loop and every time loop provide different array value and compare in if condition.
I need some method that help me find value directly in array object and return in True/False where can i perform my action
Upvotes: 3
Views: 133
Reputation: 6066
why reinvent the wheel?
$scope.$watch(function(){
return $scope.taxarr;
}, function taxarr_change(newValue, oldValue){
//do your thing!
}, true)//true is not a must, read in docs
Upvotes: 3
Reputation: 11
$scope.taxarr = [];
$checkPresent = 0;
for (var co = 0; co < count_item; co++) {
$scope.qty_amt = parseInt($scope.newData1[co].quantity) * parseInt($scope.newData1[co].rate);
$scope.tax_val1 = (parseInt($scope.qty_amt) * parseInt($scope.newData1[co].tax_value)) / 100;
$scope.taxvalue = parseInt($scope.newData1[co].tax_value);
$scope.taxid = parseInt($scope.newData1[co].tax_name);
$scope.loop = $scope.taxarr.length;
if ($scope.loop === 0) {
$scope.taxarr.push({
tax_id: $scope.taxid,
tax_name: $scope.taxvalue,
tax_amount: $scope.tax_val1
});
} else {
for (var i = 0; i < $scope.loop; i++) {
if ($scope.taxid === $scope.taxarr[i].tax_id) {
$checkPresent = 1;
break;
}
}
if($checkPresent === 1){
$scope.taxarr[i].tax_amount = parseInt($scope.taxarr[i].tax_amount) + parseInt($scope.tax_val1);
}else {
$scope.taxarr.push({
tax_id: $scope.taxid,
tax_name: $scope.taxvalue,
tax_amount: $scope.tax_val1
});
}
$checkPresent = 0;
}
console.log($scope.taxarr);
}
Upvotes: 0