19

I have the following json object :

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 ?

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)?

Thanks

1
  • Is the property home_id a unique value for all the objects of the json.HOMES array? Commented May 18, 2016 at 15:04

2 Answers 2

19

You can use Array#filter and assign the result directly to the property 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>');

Sign up to request clarification or add additional context in comments.

2 Comments

This was helpful, however I'm in a situation where I need to filter according to a list of attributes, stored in an array? More precisely, how would I modify the above if I wanted all homes with id's [1,2]? I tried return a.home_id = ['1','2']; but it doesn't work as expected.
@sc28, then you need to use either indexOf with a comparison, like return ['1','2'].indexOf(a.home_id) !== -1; or to check with includes, like return ['1','2'].includes(a.home_id);.
5

With the use of utility library Lodash, you can use the method find if home_id is unique.

Find: Iterates over elements of collection (Array|Object), returning the first element predicate returns truthy for. The predicate is invoked with three arguments: (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:

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

Filter: Iterates over elements of collection (Array|Object), returning an array of all elements predicate returns truthy for. The predicate is invoked with three arguments: (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>

1 Comment

You should actually use a filter, not a find, because the OP did specify a where clause (so by definition there might be more than one match). Also, there are much simpler matchers, both in lodash and in underscore: _.where(json.HOMES, {home_id: "2"} (underscorejs.org/#findWhere) or _.filter(json.HOMES, {home_id: "2"} (lodash.com/docs#filter).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.