我有这个数组$scope.taxarr
,我正面临一些值,如下所述。
$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);
}
我有一个数组允许我检查数组对象中的特定id,我在内部if ... else
部分面临一些问题,我检查我的id是否匹配值是更新数量,否则推送对象作为新记录
我正在使用循环,每次循环都提供不同的数组值,并在if条件下进行比较。
我需要一些方法来帮助我直接在数组对象中找到值并返回True / False,我可以在哪里执行我的操作
答案 0 :(得分:3)
为什么重新发明轮子?
$scope.$watch(function(){
return $scope.taxarr;
}, function taxarr_change(newValue, oldValue){
//do your thing!
}, true)//true is not a must, read in docs
答案 1 :(得分:0)
$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);
}