jQuery json空对象筛选替换
来源:互联网 发布:jackeylove知乎 编辑:程序博客网 时间:2024/05/24 05:31
Requirement:
一个json object,并且可能包含一些空值或者空字符串,在页面显示的时候希望遇到空值显示“N/A”,但是有一部分值是允许空值的。因此希望通过筛选将空值设为“N/A”.例如希望学生的“age”和“score”如果为空显示“N/A”,而“sex”或者“comment”为空则不做处理。
var student = { "name" : "Guo", "sex" : "", "age" : "", "num ": 01, "scores" : [ { "subject" : "English", "score" : 50, "comment" : "" }, { "subject" : "Computer", "score" : "", "comment" : "absent" } ] }; var exclude = ["sex", "comment"]; // method 1 to validate obj validateObj1 = function(obj, excluded){ var value; for(var key in obj){ value = obj[key]; if($.isArray(value)){ obj = validateArray1(obj, key, excluded); }else if(($.inArray(key, excluded) == -1) && ($.isBlank(value))){ obj[key] = "N/A"; } } return obj; } validateArray1 = function(obj, key, excluded){ var subValue; for(var i = 0, length = obj[key].length; i < length; i++){ for(var subKey in obj[key][i]){ subValue = obj[key][i][subKey]; if(($.inArray(subKey, excluded) == -1) && ($.isBlank(subValue))){ obj[key][i][subKey] = "N/A"; } } } return obj; } // method 2 to validate obj validateObj2 = function(obj, excluded){ $.each(obj ,function(key, value){ if($.isArray(value)){ obj = validateArray2(obj, key, excluded); }else if(isInvalid(key, value, excluded)){ obj[key] = "N/A"; } }); return obj; } validateArray2 = function(obj, key, excluded){ for(var i = 0, length = obj[key].length; i < length; i++){ $.each(obj[key][i] ,function(subKey, subValue){ if(isInvalid(subKey, subValue, excluded)){ obj[key][i][subKey] = "N/A"; } }); } return obj; } isInvalid = function(key, value, excluded){ return (($.inArray(key, excluded) == -1) && ($.isBlank(value))) ? true : false; } $.isBlank = function(obj){ return(!obj || $.trim(obj) === ""); };
Method 1 结果
Method 2 结果
<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- jQuery json空对象筛选替换
- Json{}对象判空
- jquery筛选数组之grep、each、inArray、map的用法及遍历json对象
- jquery筛选数组之grep、each、inArray、map的用法及遍历json对象
- jquery筛选数组之grep、each、inArray、map的用法及遍历json对象
- jquery遍历筛选数组的几种方法和遍历解析json对象
- jquery遍历筛选数组的几种方法和遍历解析json对象
- jquery筛选数组之grep、each、inArray、map的用法及遍历json对象 [转]
- jquery遍历筛选数组的几种方法和遍历解析json对象
- jquery筛选数组之grep、each、inArray、map的用法及遍历json对象
- jquery筛选数组之grep、each、inArray、map的用法及遍历json对象
- jquery遍历筛选数组的几种方法和遍历解析json对象
- $.grep ,$.map,jquery筛选数组之grep、each、inArray、map的用法及遍历json对象
- jquery筛选数组之grep、each、inArray、map的用法及遍历json对象
- Jquery遍历筛选数组的几种方法和遍历解析json对象|Map()方法详解
- jquery遍历筛选数组的几种方法和遍历解析json对象
- jquery遍历筛选数组的几种方法和遍历解析json对象
- jquery遍历筛选数组的几种方法和遍历解析json对象
- 问题一百一十七:排列
- 算法设计与分析课程机试题
- 字符串的相似度
- Machine Learning week 1 Programming Excercise
- Linux programming : Socket
- jQuery json空对象筛选替换
- POJ 1035 Spell checker
- virtual ~ClxDerived()
- 查看sql语句执行时间/测试sql语句性能
- C/C++中的 error LNKxxxx: unresolved external symbol "xxxxx"
- OPT_PARAM Hint saves the day…
- 蓝桥杯(打印回形嵌套)
- 什么是函数式编程?
- oracle常用命令