简体   繁体   English

如何使用javascript过滤复杂的json对象?

[英]How to Filter a complex json object using javascript?

I have the following json object : 我有以下json对象:

var json = {
    "Lofts": "none",
    "Maisons": "2",
    "HOMES": [{
        "home_id": "1",
        "price": "925",
        "num_of_beds": "2"
    }, {
        "home_id": "2",
        "price": "1425",
        "num_of_beds": "4",
    }, {
        "home_id": "3",
        "price": "333",
        "num_of_beds": "5",
    }]
};

How can I filter this object and remain with the HOMES property where home_id = 2 ? 如何过滤此对象并保留在home_id = 2的HOMES属性中?

Result: 结果:

var json = {
    "Lofts": "none",
    "Maisons": "2",
    "HOMES": [{
        "home_id": "2",
        "price": "1425",
        "num_of_beds": "4",
    }]
};

Is there any way I can cycle the object and mantein all the properties( also lofts and maisons)? 有什么方法可以循环对象和mantein所有的属性(也是阁楼和maisons)?

Thanks 谢谢

You can use Array#filter and assign the result directly to the property HOMES . 您可以使用Array#filter并将结果直接分配给属性HOMES

 var json = { "Lofts": "none", "Maisons": "2", "HOMES": [{ "home_id": "1", "price": "925", "num_of_beds": "2" }, { "home_id": "2", "price": "1425", "num_of_beds": "4", }, { "home_id": "3", "price": "333", "num_of_beds": "5", }] }; json.HOMES = json.HOMES.filter(function (a) { return a.home_id === '2'; }); document.write('<pre>' + JSON.stringify(json, 0, 4) + '</pre>'); 

With the use of utility library Lodash , you can use the method find if home_id is unique. 使用实用程序库Lodash ,如果home_id是唯一的,则可以使用find方法。

Find : Iterates over elements of collection (Array|Object), returning the first element predicate returns truthy for. 查找 :迭代collection元素(Array | Object),返回第一个元素predicate返回truthy。 The predicate is invoked with three arguments: (value, index|key, collection). 使用三个参数调用谓词:(value,index | key,collection)。

_.find(collection, [predicate=_.identity])

Code: 码:

 var json = {"Lofts": "none","Maisons": "2","HOMES": [{"home_id": "1","price": "925","num_of_beds": "2"}, {"home_id": "2","price": "1425","num_of_beds": "4"}, {"home_id": "3","price": "333","num_of_beds": "5"}]}; json.HOMES = [_.find(json.HOMES, {home_id: '2'})]; document.write('<pre>' + JSON.stringify(json, 0, 4) + '</pre>'); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.12.0/lodash.min.js"></script> 

If there are multiple objects with the same home_id you should make a filter : 如果有多个具有相同home_id对象,则应该创建一个过滤器

_.filter(collection, [predicate=_.identity])

Filter : Iterates over elements of collection (Array|Object), returning an array of all elements predicate returns truthy for. 过滤 :迭代collection元素(Array | Object),返回所有元素的数组谓词返回truthy。 The predicate is invoked with three arguments: (value, index|key, collection). 使用三个参数调用谓词:(value,index | key,collection)。

Code: 码:

 var json = {"Lofts": "none","Maisons": "2","HOMES": [{"home_id": "1","price": "925","num_of_beds": "2"}, {"home_id": "2","price": "1425","num_of_beds": "4"}, {"home_id": "3","price": "333","num_of_beds": "5"}]}; json.HOMES = _.filter(json.HOMES, {home_id: '2'}); document.write('<pre>' + JSON.stringify(json, 0, 4) + '</pre>'); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.12.0/lodash.min.js"></script> 

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

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