Last active
June 22, 2019 09:34
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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