0

I have a json array object $scope.products= []; and ng-click function called addRow.

I want to perform a check before push new row to array is new row is already exists in array, or not when addRow function is called.

If it already exists, then new row is not push to array.

$scope.addRow = function(){
$scope.products.push({'pro_name':$scope.pro_name,'pro_id':$scope.pro_id, 'batch_no': $scope.input_batch_no });
}
1

2 Answers 2

1

Since you have a unique identifier you can check whether the list of added rows includes such element with this identifier.

 $scope.addRow = function(product) {

    if($scope.selectedProducts.find(p => p.pro_id === product.pro_id)) {
       return;
    };

    $scope.selectedProducts.push(product);

 }

Here's a working piece of code

Sign up to request clarification or add additional context in comments.

Comments

0

Just Check if it exists already in the array:

$scope.addRow = function(){
    var exists = false;
    $scope.products.forEach(product => {
        if (product.pro_name === $scope.pro_name && product.pro_id === $scope.pro_id && product.batch_no === $scope.batch_no) {
            exists = true;
        }
    })
    if (!exists) {
        $scope.products.push({
            'pro_name':$scope.pro_name,
            'pro_id':$scope.pro_id,
            'batch_no': $scope.input_batch_no
        });
    }
}

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.