Javascript 如何使用jQuery.grep()过滤多维JSON对象
我有一个JSON对象,如下所示:Javascript 如何使用jQuery.grep()过滤多维JSON对象,javascript,jquery,json,Javascript,Jquery,Json,我有一个JSON对象,如下所示: {"data": [ {"name":"Alan","height":"171","weight":"66"}, {"name":"Ben","height":"182","weight":"90"}, {"name":"Chris","height":"163","weight":"71"} ] ,"school":"Dover Secondary" } 我想过滤JSON对象,以获得高于170和高于70的数据,然后对该对象进行排序。从中,我
{"data":
[
{"name":"Alan","height":"171","weight":"66"},
{"name":"Ben","height":"182","weight":"90"},
{"name":"Chris","height":"163","weight":"71"}
]
,"school":"Dover Secondary"
}
我想过滤JSON对象,以获得高于170和高于70的数据,然后对该对象进行排序。从中,我了解到在线性阵列上很容易实现滤波,如下所示:
arr = jQuery.grep(arr, function(element, index){
return (element > 70 && index = 'weight');
});
如何同时过滤体重和身高以获得以下信息:
{"data":
[
{"name":"Ben","height":"182","weight":"90"},
]
,"school":"Dover Secondary"
}
我想你的意思是:
那不是我真正的体重和身高!犯错误我想过滤体重和身高。能做到吗?
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
});