2
json 파일에 개체가 있는지 테스트하는 방법과 실제 이름/문자열이 특정 이름과 같은지 테스트하는 방법이 궁금합니다. 그래서 객체가있는 경우 해당 개체는 이름이 "Maten을"동일한 경우 다음 테스트 그래서 그 다음 $의 .each을 할 경우 ... 실제 속성 이름과 실제 이름을 가지고 테스트했습니다
는 말은 무엇 :JSON
...
"options": {
"95217": {
"id": 95217,
"title": "kleur",
"values": {.....}
},
"95219": {
"id": 95219,
"title": "Maat",
"values": {.....}
}, etc...
jQuery를
$.getJSON('url?format=json', function(data){
var options = [];
$.each(data.product.options, function(index, option){
// Here I want to test if object "title" equals maten and if so do the next $.each ////
if(option.hasOwnProperty('title')){
/////////////////////////////////////////////////////////
$.each(option.values, function (index, value) {
options.push('<li>'+value.title+'</li>');
});
}
});
''title 'in option'이 (가) 필요합니까? 'option'에'title' 속성이 없으면'option.title'은'undefined'가되고'undefined! == 'maten'이됩니다. 그러나 option.hasOwnProperty ('title')'을 검사하여 속성이 고유한지 확인하는 것이 좋습니다. – Oriol
@Oriol - true,'option.title'도 괜찮습니다. 나는'in' 연산자를 좋아합니다. 그러나 명확하게 프로토 타입이없는 속성이없는 JSON에서 오는 객체에 대한 hasOwnProperty가 필요하지 않습니다. – adeneo
@adeneo :이 작업을 수행하는 새로운 방법에 대한 Thx! – Meules