简体   繁体   English

使用mongo风格的语法从数组而不是Mongo集合中查询内存中的JavaScript对象?

[英]Use mongo-style syntax to query in-memory JavaScript objects from arrays instead of Mongo collections?

In mongo I can construct a query like below to return objects with height not equal to 4 from a collection. 在mongo中,我可以构造一个类似下面的查询,从集合中返回高度不等于4的对象。

var mongoQuery = { height: { "$ne": 4 } };

But say I have an in-memory array of objects and want to query from them the same way: 但是说我有一个内存中的对象数组,并希望以相同的方式从它们查询:

var myArr = [{height: 5}, {height: 4}, {height:3}]

Are there any existing libraries or ways for me to use similar syntax on arrays instead of mongo collections? 是否有任何现有的库或方法可以在数组上使用类似的语法而不是mongo集合? Eg: 例如:

var result = someUtil(myArr, {height: {"$ne": 4}});  //returns all objects with height != 4

EDIT: I don't want to do != 4 , but rather generally translate from any Mongo operator (eg $eq , $ge , etc.) 编辑:我不想做!= 4 ,而是通常从任何Mongo运算符翻译(例如$eq$ge等)

Please take a look at sift.js . 请看一下sift.js。 That is what you want. 这就是你想要的。 But use it if you really need mongodb like queries, otherwise use another library like lodash or underscore. 但是如果你真的需要像mongodb这样的查询,请使用它,否则使用另一个库,如lodash或下划线。

结帐下划线库。

var result = _.find(myArr, function(item){ return item.height == 4 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM