Skip to content

Instantly share code, notes, and snippets.

@micsen
Last active June 19, 2016 01:05
Show Gist options
  • Save micsen/1ee782f1ed081641895e76fcf0f2e85f to your computer and use it in GitHub Desktop.
Save micsen/1ee782f1ed081641895e76fcf0f2e85f to your computer and use it in GitHub Desktop.
function searchForColor(arr, arr2, color) {
var result = arr.filter(function(v) {
if(arr2.filter(function(a){ return a.fruit == v.fruit })[0]) {
return false;
} else {
return v.color === color;
}
});
return result;
}
var basketOfFruits = [
{
fruit: 'apple',
color: 'red'
},
{
fruit: 'strawberry',
color: 'red'
},
{
fruit: 'banana',
color: 'yellow'
},
{
fruit: 'pineapple',
color: 'yellow'
}
];
var eatenFruits = [
{
fruit: 'apple',
color: 'red'
}
];
//To get all red fruits not eaten
console.log(searchForColor(basketOfFruits, eatenFruits, 'red'));
//To only get the first not eaten
console.log(searchForColor(basketOfFruits, eatenFruits, 'red')[0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment