2011-09-21 18 views
10

Tengo un objeto JSON que dice así:Cómo filtrar un objeto JSON Multi-dimensión con jQuery.grep()

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

me gustaría realizar un filtrado del objeto JSON para obtener datos de los más altos que 170 y más de 70 y luego clasifica este objeto. Desde el jQuery website, entiendo que el filtrado se lograría fácilmente en una disposición lineal con algo como:

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

¿Cómo filtro tanto el peso y la altura al mismo tiempo para conseguir esto:

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

¡Ese no es mi peso y estatura reales! –

Respuesta

21

Yo te parece significa esto: 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 
}); 
+0

Err .. Quiero filtrar tanto el peso como la altura. Se puede hacer? –

+0

@Ben: Lo siento, mira mi edición por favor. – pimvdb

+0

Sí, tienes razón. Gracias ;) –

Cuestiones relacionadas