BaconJuice
BaconJuice

Reputation: 3779

how to remove object from JS array based on values from second array

Hey guys I'm trying to remove all objects from an array that matches the values from a second array. Allow me to explain with some examples!

var originalObj = [
{"id":"88","name":"Lets go testing"},
{"id":"88","name":"Have fun boys and girls"},
{"id":"108","name":"You are awesome!"},
{"id":"99","name":"Have fun boys and girls"},
{"id":"108","name":"You are awesome!"},
{"id":"99","name":"Have fun boys and girls2"},
{"id":"108","name":"You are awesome!"}
]

Currently with my javascript code I can only remove ONE specified value from object and any occurrences with said value

var updatedObj= $.grep(originalObj , function(e){ 
             return e.id!= '88'; 
        });

console.log(updatedObj)
[
    {"id":"108","name":"You are awesome!"},
    {"id":"99","name":"Have fun boys and girls"},
    {"id":"108","name":"You are awesome!"},
    {"id":"99","name":"Have fun boys and girls2"},
    {"id":"108","name":"You are awesome!"}
]

What I want to accomplish is have the .grep function look up a second array that looks something like this.

 var filterID = ["88","99"];

so the result should look something like this

//Needs updating
//var updatedObj= $.grep(originalObj , function(e){ 
//           return e.id!= '88'; <-- needs to match all filterID values instead
//      });

console.log(updatedObj)
[
    {"id":"108","name":"You are awesome!"},
    {"id":"108","name":"You are awesome!"},
    {"id":"108","name":"You are awesome!"}
]

Any help would be greatly appreciated!

Thank you.

Upvotes: 0

Views: 66

Answers (4)

Indranil Mondal
Indranil Mondal

Reputation: 2857

You could iterate over all the values in your array and filter that way.

           var updatedObj = [
                {"id":"108","name":"You are awesome!"},
                {"id":"99","name":"Have fun boys and girls"},
                {"id":"108","name":"You are awesome!"},
                {"id":"99","name":"Have fun boys and girls2"},
                {"id":"108","name":"You are awesome!"}
            ],
            filterId = ["89","99"],result=[];
            $.each(updatedObj,function(index,item){
                if(filterId.indexOf(item.id) === -1){
                   result.push(updatedObj[index])
                }
            })
            console.log(result);

Upvotes: 0

Alnitak
Alnitak

Reputation: 339856

If you use an object to represent the list of keys to filter this becomes simple:

var toRemove = {
    88: true, 99: true;  // keys significant, values thereof irrelevant
};

var updatedObj = $.grep(originalObj, function(e) { 
    return !(e.id in toRemove);
});

For short arrays this is about the same efficiency as a doubly-nested loop, that being approximately O(n * m), where n is the size of the array being filtered and m is the number of elements to remove).

However if the array of values to filter is substantial then this will be O(n) (assuming that the in operator performs in constant time)

Upvotes: 2

Patrick Evans
Patrick Evans

Reputation: 42736

use the indexOf array function

var filterID = ["88","99"];
var updatedObj= $.grep(originalObj , function(e){ 
    return filterID.indexOf(e.id) === -1; 
});

Upvotes: 0

Jnatalzia
Jnatalzia

Reputation: 1687

You could iterate over all the values in your array and filter that way.

var updatedObj = originalObj;
for (var i in filterID) {
  updatedObj = $.grep(updatedObj , function(e){ 
    return e.id!= filterID[i];
  });
}

Upvotes: 2

Related Questions