Skip to content

Instantly share code, notes, and snippets.

@gukandrew
Last active June 22, 2019 09:34
Show Gist options
  • Save gukandrew/b213c4de8782a09c83575c128612e47d to your computer and use it in GitHub Desktop.
Save gukandrew/b213c4de8782a09c83575c128612e47d to your computer and use it in GitHub Desktop.
Pure JS ES6 realization of lodash reject() method. Filter iterable object by condition object (like sql where)
function filterWhere(inputObject, conditions = {}) {
return Object.values(inputObject).filter((r) => {
return !(Object.keys(conditions).every((v) => {
return String(r[v]) === String(conditions[v])
}))
})
}
const data = {
'1': {
'id': 1,
'name': 'blah'
},
'2': {
'id': '2',
'name': 'blah2'
}
}
console.log(filterWhere(data, { 'name': 'blah2', 'id': '2' }))
// Output:
// [
// {
// 'id': 1,
// 'name': 'blah'
// }
// ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment