Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
23
rated 0 times [  29] [ 6]  / answers: 1 / hits: 21226  / 13 Years ago, wed, september 21, 2011, 12:00:00

I have a JSON object that goes like this:



{data:
[
{name:Alan,height:171,weight:66},
{name:Ben,height:182,weight:90},
{name:Chris,height:163,weight:71}
]
,school:Dover Secondary
}


I would like to filter the JSON object to obtain data of those taller than 170 and heavier than 70 and subsequently sort this object. From the jQuery website, I understand that filtering would be easily achieved on a linear array with something like:



arr = jQuery.grep(arr, function(element, index){
return (element > 70 && index = 'weight');
});


How do I filter both weight and height concurrently to get this:



{data:
[
{name:Ben,height:182,weight:90},
]
,school:Dover Secondary
}

More From » jquery

 Answers
27

I think you mean this: http://jsfiddle.net/NRuM7/1/.



var obj = {data:
[
{name:Alan,height:171,weight:66},
{name:Ben,height:182,weight:90},
{name:Chris,height:163,weight:71}
]
,school:Dover Secondary
};

obj.data = jQuery.grep(obj.data, function(element, index){
return element.weight > 70 && element.height > 170; // retain appropriate elements
});

[#90002] Monday, September 19, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zariahdiamondz

Total Points: 649
Total Questions: 109
Total Answers: 88

Location: Tajikistan
Member since Thu, Apr 14, 2022
2 Years ago
;