3

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

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
Nikunj
  • 86
  • 11
  • first, you're using angular wrong. second, If you have complex array checks and other methods to be called on arrays consider using underscore.js – astroanu Feb 15 '16 at 10:17
  • Try by `$.inArray` function provided by the jquery. https://api.jquery.com/jQuery.inArray/ – Alankar More Feb 15 '16 at 10:18
  • i use it but it's work with array and i use array object... – Nikunj Feb 15 '16 at 10:19
  • 2
    May be this answer will help you. http://stackoverflow.com/questions/11040472/check-if-object-property-exists-using-a-variable – Alankar More Feb 15 '16 at 10:20
  • let me try this ....but this is also seems like using array rather then array object ... yes I am right it's for array not for array object still problem is there... – Nikunj Feb 15 '16 at 10:21
  • 1
    @Nikunj please don't add 'help me' calls to the title of your question. It won't make anything better, it will at most attract downvotes. – Gerald Schneider Feb 15 '16 at 11:25
  • it's ok and thank you for correct me .... – Nikunj Feb 15 '16 at 11:26

2 Answers2

3

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
Reporter
  • 3,897
  • 5
  • 33
  • 47
bresleveloper
  • 5,940
  • 3
  • 33
  • 47
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);
    }
Rahul Date
  • 11
  • 3