搜索对象数组,包括子嵌套对象

时间:2016-08-20 01:05:02

标签: javascript jquery arrays object search

我想在一个带有嵌套对象的数组中搜索任何值,然后返回找到它的对象,将它放入一个新数组中。

目前我有:

sed "s/__region__/$region/" <Dockerfile.in >Dockerfile

我可以使用$ .grep进行搜索,如上所示,但是,这只搜索第一个对象,而不是嵌套...即如何扩展$ .grep的使用来迭代&#39;类型&#39; - 例如说要获得msn&#39; ??

HELP PLEASE :) :)在过去的6个小时里拔头发了!

1 个答案:

答案 0 :(得分:0)

您可以使用数组过滤器()来完成此任务。请查看以下内容:

&#13;
&#13;
var json = {
    'offices': [{
        "home_id": "1",
        "price": "925",
        "sqft": "1100",
        "num_of_beds": "2",
        "num_of_baths": "2.0",
        "types": {
            "0": "Internet",
            "1": "msn",
            "2": "aol"
        }
    }, {
        "home_id": "2",
        "price": "1425",
        "sqft": "1900",
        "num_of_beds": "4",
        "num_of_baths": "2.5",
        "types": {
            "0": "Internet",
            "1": "google",
            "2": "virgin"
        }
    }, {
        "home_id": "1",
        "price": "925",
        "sqft": "1980",
        "num_of_beds": "6",
        "num_of_baths": "9.5",
        "types": {
            "0": "InterNope",
            "1": "google",
            "2": "virgin"
        }
    }
  ]
}

var theOffices = json.offices;

var results = theOffices.filter( function(f) {
  return f.home_id == "1" && f.price == "925";
});

console.log("res");
console.log(results);
&#13;
&#13;
&#13;