答案 0 :(得分:5)
1:这将删除任何假名值,即Null,undefined或空字符串。你可以专门检查空值。请确保您已阅读并了解删除操作的内容,这会让很多人遇到麻烦。
for(var key in someObject) {
if(!someObject[key]) {
delete someObject[key];
}
}
2:你可以像这样迭代对象的所有属性和值:
for(var key in someObject) {
console.log("The value of " + key + " is " + someObject[key]);
}
答案 1 :(得分:2)
对于初学者来说,修复你的javascript对象,因为你发布的内容充满了错误。一旦你有一个有效的数组:
var values = [{
'SPO2': 222.00000,
'VitalGroupID': 1152,
'Temperature': 36.6666666666667,
'DateTimeTaken': '/Date(1301494335000-0400)/',
'UserID': 1,
'Height': 182.88,
'UserName': null,
'BloodPressureDiastolic': 80,
'Weight': 100909.090909091,
'TemperatureMethod': 'Oral',
'Resprate': null,
'HeartRate': 111,
'BloodPressurePosition': 'Standing',
'VitalSite': 'Popliteal',
'VitalID': 1135,
'Laterality': 'Right',
'HeartRateRegularity': 'Regular',
'HeadCircumference': '',
'BloodPressureSystolic': 120,
'CuffSize': 'XL'
}, {
'SPO2': 100.00000,
'VitalGroupID': 1113,
'Temperature': 32.7777777777778,
'DateTimeTaken': '/Date(1299856980000-0500)/',
'UserID': 1,
'Height': 0,
'UserName': 'Admin',
'BloodPressureDiastolic': 78,
'Weight': 49895.1607,
'TemperatureMethod': '',
'Resprate': null,
'HeartRate': null,
'BloodPressurePosition': 'Sitting',
'VitalSite': '',
'VitalID': 1096,
'Laterality': '',
'HeartRateRegularity': 'Regular',
'HeadCircumference': '',
'BloodPressureSystolic': 120,
'CuffSize': ''
}];
然后你可以循环它:
for (var i = 0; i < values.length; i++) {
// this will run for each element of the initial array
for (var propertyName in values[i]) {
// this will run for each property of the element
var propertyValue = values[i][propertyName];
if (propertyValue == null) {
// if the value is null remove it
delete values[i][propertyName];
} else {
console.log('name: ' + propertyName + ', value: ' + propertyValue);
}
}
}
答案 2 :(得分:1)
删除属性:
if (objectName.propertyName === null) {
delete objectName.propertyName;
}
迭代投掷属性:
for (var key in objectName) {
document.write(objectName[key]);
}
答案 3 :(得分:1)
var array_of_json_hashes;
var result = [];
for(var i = 0; i < array_of_json_hashes.length; i++) {
result[i] = {};
var h = array_of_json_hashes[i];
for (var key in h) {
console.log(key);
console.log(h[key]);
if (h.hasOwnProperty(key)) {
if(h[key]) {
result[i][key] = h[key];
}
}
}
}
console.log(result);
答案 4 :(得分:0)
这可能对您有所帮助:
1)test = [“1”,“2”,“3”,“4”,“”,“”];
2)var delete = JSON.stringify(test);
case1)delete = delete.replace(/ \,“”/ g,'');
或
case2)delete = delete.replace(/ \,“”/ g,'');
或
case3)delete = delete.replace(/ \,null / g,'');
3)var result = JSON.parse(delete);