I'm pulling my hair out over this one. I have two arrays, likes
& dislikes
, both filled with about 50 strings each.
I also have a JSON object, data.results
, which contains about 50 objects, each with an _id
parameter.
I'm trying to check find all the objects within data.results
that aren't in both likes
and dislikes
.
Here's my code at present:
var newResults = []
for(var i = 0; i<data.results.length; i++){
for(var x = 0; x<likes.length; x++){
if(!(data.results[i]._id == likes[x])){
for(var y = 0; y<dislikes.length; y++){
if(!(data.results[i]._id == dislikes[y])){
newResults.push(data.results[i]);
console.log("pushed " + data.results[i]._id);
}
else
{
console.log("They already HATE " + data.results[i]._id + " foo!"); //temp
}
}
}
else
{
console.log(data.results[i]._id + " is already liked!"); //temp
}
}
}
As you can see, I'm iterating through all the data.results
objects. Then I check whether their _id
is in likes
. If it isn't, I check whether it's in dislikes
. Then if it still isn't, I push it to newResults
.
As you might expect by looking at it, this code currently pushes the result into my array once for each iteration, so i end up with a massive array of like 600 objects.
What's the good, simple way to achieve this?