如何使用jQuery.grep()过滤多维JSON对象 [英] How to filter a Multi-dimension JSON object with jQuery.grep()

查看:53
本文介绍了如何使用jQuery.grep()过滤多维JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON对象,如下所示:

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"
}

我想过滤JSON对象以获取数据那些高于170且重于70的人,然后对这个物体进行排序。从 jQuery网站中,我了解到可以在线性阵列上轻松实现过滤,例如:

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"
}


推荐答案

我认为你的意思是: 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
});

这篇关于如何使用jQuery.grep()过滤多维JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆