Reputation: 6868
Below is my json structure:
$scope.dataList = [{
CompanyName: null,
Location: null,
Client: [{
ClientId: 0,
ClientName: null,
Projects:{
Id: 0,
Name: null,
}
}]
}];
I am trying to remove client data by
specific clientid from list of clients
but client data is not getting removed and i am not getting any kind of error.
Code:
for (var i = 0; i < $scope.dataList.length; i++)
{
for (var j = 0; j < $scope.dataList[i].Client.length; j++)
{
if ($scope.dataList[i].Client[j].ClientId == 101)
{
$scope.dataList[i].Client.splice(j, 1);
}
}
}
Can anybody tell me what is the problem with my code??
Upvotes: 0
Views: 78
Reputation: 4913
This works:
for (var i = 0; i < $scope.dataList.length; i++) {
for (var j = 0; j < $scope.dataList[i].Client.length; j++) {
var foundIndex;
if ($scope.dataList[i].Client[j].ClientId == 101){
foundIndex = j;
}
$scope.dataList[i].Client.splice(j, 1);
}
}
Fiddle: https://jsfiddle.net/uv3zo0y2/
Upvotes: 1
Reputation: 1679
You can use delete statement for this.
for (var i = 0; i < $scope.dataList.length; i++)
{
for (var j = 0; j < $scope.dataList[i].Client.length; j++)
{
if ($scope.dataList[i].Client[j].ClientId == 101)
{
delete $scope.dataList[i].Client[j];
}
}
}
But this will be create problem when you are delete because in for loop one item delete so item count decrease.
So you have to use other way for this.
Upvotes: 1