Khalid Khalid - 3 months ago 8
AngularJS Question

How to remove particular element in angularjs if its in json data?

I am using angular js and ionic framework.A problem is i need to remove a element from the jsondata which comes from API.

Json data :

[["abcd@gmail.com","joe"],["xyz@gmail.com","brain"],["hhh@gmail.com","bob"]];


i need to remove 2nd element "xyz@gmail.com" and dislay only the remaining elements

[["abcd@gmail.com","joe"],["hhh@gmail.com","bob"]];


Controller condition:

if(response.RETURN == "TRUE") {
var listObject = JSON.parse(response.LIST);
$scope.ulist = listObject;
//HERE I NEED TO FILTER THAT PARTICULAR ELEMENT
}


Thanks in advance

Joy Joy
Answer

Just a for loop will do it, right?

$scope.ulist = [];
for (var i = 0, len = listObject.length; i < len; i++ ) {
    if(listObject[i][0] != "xyz@gmail.com") {
        $scope.ulist.push(listObject[i]);
    }
}

Alternatively, use Array.filter:

$scope.ulist = listObject.filter(function (email) {
     return email[0] !== "xyz@gmail.com";
});
Comments