underscore has a where function that only does this
var _ = require("underscore"); var json = '[{"user": "a", "age": 20}, {"user": "b", "age": 30}, {"user": "c", "age": 40}]'; var users = JSON.parse(json); var filtered = _.where(users, {user: "a"}); // => [{user: "a", age: 20}]
Another Lo-Dash utility library has a where function, which works the same way.
You can add underline to your project using
$ npm install
or lodash
$ npm install
If you only care about the where function, lodash offers it as a separate module
// only install lodash.where $ npm install
To use it in your project
var where = require("lodash.where"); // ... var filtered = where(users, {"user": "a"});
Even if you use the library for this, itβs best to set up a thread chain that processes all your data processing in smaller modules.
Not knowing what you really want to do, I created this as an example. For the purposes of this code, perhaps think of a debug logging thread or something like that.
JSON-parser.js
input: string (JSON)
output: object
var Transform = require("stream").Transform; function JsonParser() { Transform.call(this, {objectMode: true}); this._transform = function _transform(json, enc, done) { try { this.push(JSON.parse(json)); } catch (e) { return done(e); } done(); } } JsonParser.prototype = Object.create(Transform.prototype, { constructor: { value: JsonParser } }); module.exports = JsonParser;
Obj-filter.js
input: object
output: object (result where(data, filters) )
var Transform = require("stream").Transform; var where = require("lodash.where"); function ObjFilter(filters) { Transform.call(this, {objectMode: true}); this._transform = function _transform(obj, enc, done) { this.push(where(obj, filters)); done(); } } ObjFilter.prototype = Object.create(Transform.prototype, { constructor: { value: ObjFilter } }); module.exports = ObjFilter;
stringifier.js
input: object
output: string (JSON)
var Transform = require("stream").Transform; function Stringifier() { Transform.call(this, {objectMode: true}); this._transform = function _transform(obj, enc, done) { this.push(JSON.stringify(obj)); done(); } } Stringifier.prototype = Object.create(Transform.prototype, { constructor: { value: Stringifier } }); module.exports = Stringifier;
app.js
// modules var JsonParser = require("json-parser"); var ObjFilter = require("obj-filter"); var Stringifier = require("stringifier"); // run var parser = new JsonParser(); // setup stream chain parser.pipe(new ObjFilter({"user": "a"})) .pipe(new Stringifier()) .pipe(process.stdout); // send example json in parser.write('[{"user": "a", "age": 20}, {"user": "b", "age": 30}, {"user": "c", "age": 40}]'); // output // => [{"user":"a","age":20}]
Here I created a Stringifier stream that converts objects back to JSON so that we can see how they are dumped to the console, although you can easily create any threads necessary to handle the operations your application requires. The endpoints of your stream will most likely not be written to the console.
As a final note, you are likely to create a database stream that takes some query parameters and emits json. You will pass this stream directly to parser .
In any case, I hope this gives you a better idea of ββhow to process data in node.js.