Below is the json :
以下是json:
var data= {
'A' : {
'Total' : 123,
'Cricket' : 76,
'Football' : 12,
'Hockey' : 1,
'None' : 10
},
'B' : {
'Total': 123,
'Cricket': 76,
'Football': 12,
'Hockey': 1,
'None': 10
},
'C' : {
'Total': 0,
'Cricket': 76,
'Football': 12,
'Hockey': 1,
'None': 10
}
}
I want to remove C in which value of total is zero,,
我想删除总值为零的C,
$.each(json, function (key, value) {
if (value.Total === 0) {
//delete
}
});
I have tried various ways but didnt work
我尝试了各种方法,但没有工作
2 个解决方案
#1
6
Use delete
keyword to delete a property in an object.
使用delete关键字删除对象中的属性。
var json = {
'A': {
'Total': 123,
'Cricket': 76,
'Football': 12,
'Hockey': 1,
'None': 10
},
'B': {
'Total': 123,
'Cricket': 76,
'Football': 12,
'Hockey': 1,
'None': 10
},
'C': {
'Total': 0,
'Cricket': 76,
'Football': 12,
'Hockey': 1,
'None': 10
}
}
$.each(json, function(key, value) {
if (value.Total === 0) {
delete json[key];
}
});
console.log(json);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
#2
0
$(function(){
var data= {
'A' : {
'Total' : 123,
'Cricket' : 76,
'Football' : 12,
'Hockey' : 1,
'None' : 10
},
'B' : {
'Total': 123,
'Cricket': 76,
'Football': 12,
'Hockey': 1,
'None': 10
},
'C' : {
'Total': 0,
'Cricket': 76,
'Football': 12,
'Hockey': 1,
'None': 10
}
}
$.each(data, function (key, value) {
if (value.Total === 0) {
delete data[key];
}
});
console.log(data);
});
Fiddle: http://jsfiddle.net/ef3Le1en/
#1
6
Use delete
keyword to delete a property in an object.
使用delete关键字删除对象中的属性。
var json = {
'A': {
'Total': 123,
'Cricket': 76,
'Football': 12,
'Hockey': 1,
'None': 10
},
'B': {
'Total': 123,
'Cricket': 76,
'Football': 12,
'Hockey': 1,
'None': 10
},
'C': {
'Total': 0,
'Cricket': 76,
'Football': 12,
'Hockey': 1,
'None': 10
}
}
$.each(json, function(key, value) {
if (value.Total === 0) {
delete json[key];
}
});
console.log(json);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
#2
0
$(function(){
var data= {
'A' : {
'Total' : 123,
'Cricket' : 76,
'Football' : 12,
'Hockey' : 1,
'None' : 10
},
'B' : {
'Total': 123,
'Cricket': 76,
'Football': 12,
'Hockey': 1,
'None': 10
},
'C' : {
'Total': 0,
'Cricket': 76,
'Football': 12,
'Hockey': 1,
'None': 10
}
}
$.each(data, function (key, value) {
if (value.Total === 0) {
delete data[key];
}
});
console.log(data);
});
Fiddle: http://jsfiddle.net/ef3Le1en/