Reputation: 184
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 });
}
Upvotes: 0
Views: 274
Reputation: 2682
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
Upvotes: 1
Reputation: 15765
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
});
}
}
Upvotes: 0