gpt4 book ai didi

javascript - 有没有办法使用 MongoDB 查询对象来过滤常规 JavaScript 数组?

转载 作者:可可西里 更新时间:2023-11-01 02:58:18 25 4
gpt4 key购买 nike

在 MongoDB 中,您可以使用如下所示的 JSON 样式对象来查询集合:

db.things.find({ x : { $ne : 3 }, y : 'foo' });

我想重用那个 { x : { $ne : 3 }, y : 'foo' } 位并用它来过滤 JavaScript 对象数组。

是否有任何代码/库可以做到这一点,并且支持所有查询选项(或者尽可能多地有意义)?

最佳答案

好的,这是另一个尝试:

sift.js (npm: sift) by Craig Condon is a MongoDB-inspired arrayfiltering library. It’s a bit like an alternative to Underscore forpeople who love MongoDB. Sift.js supports operators like $in and $gt,but can also filter arrays based on functions and even works withdeeply-nested objects in arrays.

Craig has provided a few examples that should look familiar to Mongousers:

var sift = require('sift');

sift({ $in: ['hello','world'] }, ['hello','sifted','array!']); //
['hello']

来源(已编辑):Daily JS,但网站似乎已关闭。

关于javascript - 有没有办法使用 MongoDB 查询对象来过滤常规 JavaScript 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8695718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com