- 如何刪除鍵值爲空值並形成新的json。
- 如何迭代這個響應....在哪裏我想顯示..這個鍵 ==>相應的值。
2
A
回答
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);
}
}
}
Demo。
5
1:這將刪除任何錯誤的值,即空值,未定義或空字符串。儘管你可以專門檢查空值。確保你閱讀並理解刪除操作,這會讓很多人陷入困境。
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]);
}
+2
這不會刪除假鑰嗎? –
1
刪除屬性:
if (objectName.propertyName === null) {
delete objectName.propertyName;
}
迭代扔性質:
for (var key in objectName) {
document.write(objectName[key]);
}
+0
'undefined'和'null'具有相似的語義。 使用'=='更穩定 –
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);
相關問題
- 1. 刪除具有空白值的鍵
- 2. 按值和鍵排序
- 3. 刪除SQLite的一排空
- 4. 刪除排序
- 5. 刪除空鍵的PowerShell
- 6. PHP排序值和相等的值的陣列,排序鍵
- 7. 從js數組中刪除值而不重新排序鍵
- 8. 如何刪除空白和重新排序的話
- 9. JAVA ArrayList的排序和刪除對
- 10. PHP - 數組中基於鍵的值和空值刪除重複值
- 11. 刪除列排序
- 12. ArrayCollection刪除排序
- 13. 排除DataView中的空值
- 14. 刪除空值POST
- 15. SQL Server:主鍵自動增量 - 刪除的行和空閒鍵值是什麼?
- 16. json4s刪除鍵的值無
- 17. 排序的價值元素和刪除重複
- 18. 刪除/排除在PHP中保存和使用的空行
- 19. 的jQuery 1.3.2刪除空值
- 20. PHP - 排序和刪除重複?
- 21. 排序字符串和刪除字符
- 22. 排序和從列表中刪除Python
- 23. PHP刪除空,空的數組鍵/值,同時保持鍵/值,否則不是空/空
- 24. hibernate:排序空值
- 25. 不能刪除,但需要和空值
- 26. 排除空meta_key值導致
- 27. 是否DISTINCT排除空值
- 28. 獲取排序鍵和值在地圖
- 29. 對值和鍵進行NSMutableDictonary排序?
- 30. 按鍵和值排序字典?
我如何知道對象的鍵的總數... –
@John Cooper,請看這裏:http://stackoverflow.com/questions/126100/how-to -efficiently計數最數的密鑰的屬性-的-AN-對象中的JavaScript –