I found myself wanting to query an array of in-memory objects in my NodeJS app. I know there are a ton of options out there… including the built-in filter method on arrays, underscore or lodash where methods, and more. But I really wanted something that worked like MongoDB queries.
What I Want To Do
Given this data structure:
I wanted to write a query that looked something like this:
sift.js: How I Got What I Wanted
It’s pretty slick. It let’s me write and execute the above query, running it against my array of objects:
This returns the second object, as I expected.
Lots Of Options
From the documentation, it looks like it should work in browsers as well as nodejs environments. Of course, this all makes me wonder if MongoDB could create a portable version of their query library to use in NodeJS… but I’m liking sift so far. It does what I want, and seems small / simple enough.
I’m sure there are plenty of ways to achieve close-to this syntax with other libraries like underscore / lodash, or other JSON / JS query libraries, too. I picked sift because I like that it models after MongoDB queries. I’m already familiar with that syntax, and I’ll be able to take advantage of it as I need more complex queries.