在jQuery中 關于json空對象篩選替換
更新時間:2013年04月15日 09:16:38 作者:
本篇文章,小編將為大家介紹,在jQuery中 關于json空對象篩選替換,有需要的朋友可以參考一下
Requirement:
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) === "");
};
一個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 結果
相關文章
TimergliderJS 一個基于jQuery的時間軸插件
Timeglider JS是一個由javascript支持縮放,數據驅動的時間軸組件。非常適合顯示項目歷史,項目計劃及其其它需要顯示歷史的項目2011-12-12jQuery EasyUI Accordion可伸縮面板組件使用詳解
這篇文章主要為大家詳細介紹了jQuery EasyUI Accordion可伸縮面板組件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02jQuery+Ajax+PHP+Mysql實現分頁顯示數據實例講解
這是一個典型的Ajax應用,在頁面上,您只需要點擊“下一頁”,數據區(qū)將自動加載對應頁碼的數據,重新刷新數據區(qū)。類似的效果在很多網站上應用,尤其在一些需要展示大量圖片數據的網頁如淘寶商品列表頁,Ajax分頁效果讓您的網站數據加載顯得非常流暢。2015-09-09