2011-07-17 4 views
2
  • 어떻게 null 값을 갖는 키를 제거하고 새 json을 형성 할 수 있습니까?
  • 이 응답을 반복 할 수있는 방법 .... 표시 할 곳 ...이 키 ==> 해당 값.

답변

2

:

if (objectName.propertyName === null) { 
    delete objectName.propertyName; 
} 

을 순회 속성을 던질 우선 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.

+0

을 사용하는 것이 더 안정적입니다. 객체의 총 키 수를 알 수있는 방법은 ... –

+0

@ John Cooper, 여기를 참조하십시오 : http://stackoverflow.com/questions/126100/how-to - 효율적으로 - 개수 - of-object-of-object-in-javascript –

5

1 : 이것은 잘못된 값, 즉 Null, 정의되지 않은 문자열 또는 빈 문자열을 제거합니다. 당신은 nulls를 위해 특히 검사 할 수 있었다. 읽고 삭제 한 내용을 이해하면 많은 사람들이 곤경에 빠지게됩니다.

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

은 속성 삭제 :

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);