Skip to content

Instantly share code, notes, and snippets.

@fiote
Created June 30, 2022 23:48
Show Gist options
  • Save fiote/90d81a800dc9d748ba89779c01d50d45 to your computer and use it in GitHub Desktop.
Save fiote/90d81a800dc9d748ba89779c01d50d45 to your computer and use it in GitHub Desktop.
const filters = {
tamanho: 'G',
cor: 'red'
};
const validkeys = ['tamanho','cor'];
const wheres = [];
const params = [];
Object.keys(filters).forEach(key => {
if (!validkeys.includes(key)) return;
wheres.push(`${key} = ?`);
params.push(filters[key]);
});
const result = await mysql.query(`
SELECT *
FROM camisas
WHERE ${wheres.join(' OR ')}
`, params);
const matches = result.rows.map(row => {
let qtmatches = 0;
Object.keys(filters).forEach(key => {
if (!validkeys.includes(key)) return;
if (row[key] == filters[key]) qtmatches++;
});
return qtmatches;
});
console.log(matches);
// [2,1,0];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment