0

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 :

 [["[email protected]","joe"],["[email protected]","brain"],["[email protected]","bob"]];

i need to remove 2nd element "[email protected]" and dislay only the remaining elements

  [["[email protected]","joe"],["[email protected]","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

4 Answers 4

2

Just a for loop will do it, right?

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

Alternatively, use Array.filter:

$scope.ulist = listObject.filter(function (email) {
     return email[0] !== "[email protected]";
});
Sign up to request clarification or add additional context in comments.

Comments

2

The simplest solution is using splice method of Javascript.

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

Comments

0

the easiest way to handle this is to use lodash library, it will save your time and make your code readable.

_.pullAt(array, [indexes])

Removes elements from array corresponding to the given indexes and returns an array of the removed elements. Indexes may be specified as an array of indexes or as individual arguments.

 var array = [5, 10, 15, 20];
    var evens = _.pullAt(array, 1, 3);

    console.log(array);
    // → [5, 15]
    console.log(evens);
    // → [10, 20]

and in your case:

var listOfFilteresObjects = _.pullAt(listOfObjects,2); 

and thats all bro,

Cheers

Comments

0
   var testArr = [["[email protected]", "joe"],
                  ["[email protected]", "brain"],
                  ["[email protected]", "bob"]];

   var customFilter = function(arr, filterStr) {
       var result = [];
       angular.forEach(arr, function (a1) {
           var tmp = a1;
           var idx = 0;
           angular.forEach(tmp, function (a2) {
               if ((a2 !== filterStr)
               && (idx === 0)) {
                   result.push(tmp);
               }
               idx++;
           });
       });
       return result;
   }

   var filteredTestArr = customFilter(testArr, "[email protected]");

1 Comment

Welcome to Stack Overflow! Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.

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.