Javascript 过滤对象数组

Javascript 过滤对象数组,javascript,object,Javascript,Object,我通过API从MongoDB获得一个对象数组 然后我需要进一步过滤结果(客户端) 我将使用长列表(可能是数千个结果),每个对象大约有10个属性,其中包含一些数组 对象的示例: { _id: xxxxxxx, foo: [ { a: "b", c: "d" }, { a: "b", c: "d" } ], data: { a: "b", c: "d" } } 我异步循环阵列以提高速度: async.filter(documents, f

我通过API从MongoDB获得一个对象数组

然后我需要进一步过滤结果(客户端)

我将使用长列表(可能是数千个结果),每个对象大约有10个属性,其中包含一些数组

对象的示例:

{
  _id: xxxxxxx,
  foo: [
    { a: "b", c: "d" },
    { a: "b", c: "d" }    
  ],
  data: {
    a: "b",
    c: "d"
  }
}
我异步循环阵列以提高速度:

async.filter(documents, function(value) {
  // Search inside the object to check if it contains the given "value"
}, function(results) {
  // Will do something with the result array
});

如何在当前对象中搜索以检查其是否包含给定值,而不知道在哪个属性中可以找到该值?

虽然我没有包含
async
部分,但我相信总体搜索方法可能是这样的:

//输入数组
var inpArr=[{
id:1,
傅:[{
a:“狗”,
b:“猫”
}]
}, {
id:2,
傅:[{
答:“库塔”,
b:“比利”
}]
}];
var myFilter=函数(val、项、索引、数组){
var searchResult=scanProperties(项目,val);
返回搜索结果;
};
//注意:将附加参数传递给默认筛选器。
//使用Function.Prototype.Bind
var filterResult=inpArr.filter(myFilter.bind(null,“dog”);
警报(filterResult);
console.log(filterResult);
//递归扫描所有属性
函数属性(obj、val){
var结果=假;
for(obj中的var属性){
if(obj.hasOwnProperty(property)&&obj[property]!=null){
if(obj[property].constructor==Object){
结果=结果| |扫描属性(obj[property],val);
}else if(obj[property].constructor==Array){
对于(var i=0;i};您可以简单地递归遍历每个项目,如下所示

var data = {
    _id: 1243,
    foo: [{
        a: "b",
        c: "d"
    }, {
        a: "b",
        c: "d"
    }],
    data: {
        a: "b",
        c: "d"
    }
};

function findValue(value) {
    function findItems(document) {
        var type = Object.prototype.toString.call(document);
        if (type.indexOf("Array") + 1) {
            return document.some(findItems);
        } else if (type.indexOf("Object") + 1) {
            return Object.keys(document).some(function(key) {
                return findItems(document[key]);
            });
        } else {
            return document === value;
        }
    }
    return findItems;
}
console.log(findValue('dd')(data));
# false
console.log(findValue('d')(data));
# true

假设您要查找的值是
d
,那么预期的输出是什么?是真还是假,取决于该值是否存在