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

7
  • 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 Commented Feb 15, 2016 at 10:17
  • Try by $.inArray function provided by the jquery. api.jquery.com/jQuery.inArray Commented Feb 15, 2016 at 10:18
  • i use it but it's work with array and i use array object... Commented Feb 15, 2016 at 10:19
  • 2
    May be this answer will help you. stackoverflow.com/questions/11040472/… Commented Feb 15, 2016 at 10:20
  • 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. Commented Feb 15, 2016 at 11:25

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

docs.angularjs.org/api/ng/type/$rootScope.Scope#$watch - and yes, and since its an array leave the "true"
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);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.