- How can i remove the key's with null values and form a new json.
- How can iterate this response...开发者_运维知识库. where i want to display.. this key ==> corresponding value.
1: This will remove any falsey value i.e. Null, undefined or empty strings. You could specifically check for nulls though. Make sure you read up and understand what delete does, it gets a lot of people in trouble.
for(var key in someObject) {
if(!someObject[key]) {
delete someObject[key];
}
}
2: You can iterate all properties and values of objects like so:
for(var key in someObject) {
console.log("The value of " + key + " is " + someObject[key]);
}
For starters fix your javascript object as what you have posted is full of errors. Once you have a valid array:
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': ''
}];
then you could loop through it:
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);
}
}
}
Demo.
Deleting a property:
if (objectName.propertyName === null) {
delete objectName.propertyName;
}
Iterating throw properties:
for (var key in objectName) {
document.write(objectName[key]);
}
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);
This may be helpful for you:
1) test = ["1","2","3","4",""," "];
2) var delete = JSON.stringify(test);
case1) delete = delete.replace(/\,""/g,'');
or
case2) delete = delete.replace(/\," "/g,'');
or
case3) delete = delete.replace(/\,null/g,'');
3) var result = JSON.parse(delete);
精彩评论