Skip to content

Instantly share code, notes, and snippets.

@cfjedimaster
Created June 27, 2024 21:08
Show Gist options
  • Save cfjedimaster/4070cbeb3665630a297f84ed6d68c767 to your computer and use it in GitHub Desktop.
Save cfjedimaster/4070cbeb3665630a297f84ed6d68c767 to your computer and use it in GitHub Desktop.
/*
So, I struggled with the logic of - given we have 2 or 3 arrays, what items exist in ALL 3
Idea by @falken on Masto
*/
let numArrays = 0;
if(filter.title) numArrays++;
if(filter.duration) numArrays++;
if(filter.ingredient) numArrays++;
let idBag = {};
let obBag = {};
if(filter.title) {
for(t of byTitle) {
if(!idBag[t.id]) idBag[t.id] = 1;
else idBag[t.id]++;
obBag[t.id] = t;
}
}
if(filter.duration) {
for(t of byDuration) {
if(!idBag[t.id]) idBag[t.id] = 1;
else idBag[t.id]++;
obBag[t.id] = t;
}
}
if(filter.ingredient) {
for(t of byIngredient) {
if(!idBag[t.id]) idBag[t.id] = 1;
else idBag[t.id]++;
obBag[t.id] = t;
}
}
for(let id in idBag) {
if(idBag[id] === numArrays) result.push(obBag[id]);
}
resolve(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment