gpt4 book ai didi

javascript - 如何过滤 Meteor 数据库查询结果?

转载 作者:可可西里 更新时间:2023-11-01 10:04:42 24 4
gpt4 key购买 nike

我很难找到一种优雅的方法来过滤我不想要的对象数组的 mongodb 查询结果。

我得到一个对象数组:

var articles = Tips.find().fetch();

而且我还有几篇文章已经被选中了,应该归还

var selected = [{Object}, {Object}];

我很难相信没有内置函数,例如:

articles.remove(selected);

但是没有,考虑到我们在 Meteor 中使用 MongoDb 的数量,我想有人已经找到了一些很好的辅助函数来完成这个和其他类似的功能

谢谢

最佳答案

所以我找到了一个合理的解决方案,但它不完整:

Array.prototype.removeObjWithValue = function(name, value){
var array = $.map(this, function(v,i){
return v[name] === value ? null : v;
});
this.length = 0; //clear original array
this.push.apply(this, array); //push all elements except the one we want to delete
}

Array.prototype.removeObj = function(obj){
var array = $.map(this, function(v,i){
return v["_id"] === obj["_id"] ? null : v;
});
this.length = 0; //clear original array
this.push.apply(this, array); //push all elements except the one we want to delete
}

我仍然遇到的问题是这不起作用并不断返回 []

Array.prototype.removeObjs = function(objs){
var array = this;
console.log(array);
$.each(objs, function (i,v) {
array.removeObj(v);
console.log(array);
})
console.log(array);
this.length = 0; //clear original array
this.push.apply(this, array); //push all elements except the ones we want to delete
}

关于javascript - 如何过滤 Meteor 数据库查询结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18477318/

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