2016-10-21 2 views
0

우선 나는 아직도 배우기 때문에 친절하게하십시오. json 응답에서 특정 요소를 선택하고 싶습니다. 개체 안에 특정 이름의 요소를 가져 오려고하고 있는데 미리 정의 된 이름과 일치하는지 테스트하려고합니다. "길이", "너비"와 같은 구체적인 이름의 제품 사양입니다.오브젝트의 이름을 선택하십시오.

이러한 특정 이름을 찾지 못하고 "값"속성을 얻는 데 어려움이 있습니다.

는 그래서 내가 가진 것은 이것이다 :

JSON 응답 :

{ 

"product": { 
    "specs": { 

     "231638": { 
     "id": 231638, 
     "title": "Length (mm)", 
     "value": "1200" 
    }, 
    "231641": { 
     "id": 231641, 
     "title": "Width (mm)", 
     "value": "800" 
    }, 
     "231644": { 
     "id": 231644, 
     "title": "Height (mm)", 
     "value": "144" 
    } //etc etc 

가 지금은 사양 "길이"와 "높이"만 두 value 특성을 잡아 싶어요. 그래서 먼저 두 개의 이름이 존재하고 "길이"와 "높이"가 일치하는지 살펴 봐야하고, 그렇다면 그들의 value 속성을 가져와야합니다. 그것이 도움이 필요한 곳입니다.

최종 결과는

<div class="somediv"> 
<span class="width">1200</span> 
<span class="height">144</span> 
</div> 

이 그래서 내가 가진 것은 이것이다이어야합니다

$.getJSON(url', function(data){ 
     var specsHtml = []; 

     $.each(data.product.specs, function(index, spec){ 
     // in here I need to test if the name of the spec equals "length or Height". 
     specsHtml.push(spec.value); 
     }); 
     specsHtml = specsHtml.join(''); 
     } 
    container.find('.product-bottom .measurements span').html(specsHtml); 
}); 

여기에서 completly 붙어있어. 내가 좋아하는 일을 시도 :

(spec.hasOwnProperty("title")); 

또는

var specs = []; 
     data.product.specs.forEach(data.product.specs => { 
     if (data.product.specs) 
     for (var spec in data.product.specs) 
      specs.push(spec); 
     }) 

어떤 도움을 크게 감사합니다 :) 답변에 대한

답변

1

var data = { 
 
    "product": { 
 
    "specs": { 
 
     "231638": { 
 
     "id": 231638, 
 
     "title": "Length (mm)", 
 
     "value": "1200" 
 
     }, 
 
     "231641": { 
 
     "id": 231641, 
 
     "title": "Width (mm)", 
 
     "value": "800" 
 
     }, 
 
     "231644": { 
 
     "id": 231644, 
 
     "title": "Height (mm)", 
 
     "value": "144" 
 
     } //etc etc 
 
    } 
 
    } 
 
}; 
 

 
var length = 0, width = 0, height = 0, 
 
    reLength = /length/i, 
 
    reWidth = /width/i, 
 
    reHeight = /height/i; 
 
$.each(data.product.specs, function (specId, spec) { 
 
    if (reLength.test(spec.title)) 
 
    length = spec.value; 
 
    else if (reWidth.test(spec.title)) 
 
    width = spec.value; 
 
    else if (reHeight.test(spec.title)) 
 
    height = spec.value; 
 
}); 
 

 
var html = '<div class="somediv">' + 
 
    '<span class="width">w: ' + width + '</span>' + 
 
    '<span class="height">h: ' + height + '</span>' + 
 
    '<span class="length">l: ' + length + '</span>' + 
 
    '</div>'; 
 
$(document.body).html(html);
.somediv > span { padding: 10px; display:inline-block; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

들으.! 그러나 이것은 모든 필드에 대해 "0"을 반환합니다. 너는/길이/내가 무엇을하는지 설명해 줄 수 있니? – Meules

+0

좋아, 내가 잘못 생각하는 것 같아. 해당 regEx (예 : width)로'spec.title '을 테스트하십시오. 그러나 그것은 그 spec의'value name '과 비교 될 필요가 있기 때문에 항상 false가 될 것이다. 그걸 도울 수 있니? – Meules

+0

@Meules, 코드를 실행 가능한 스 니펫으로 대체했습니다. 그것을 확인하십시오 –

0
$.getJSON(url, function(data){ 
     var specsHtml = "<div class="somediv">"; 

     $.each(data.product.specs, function(){ 
     //'this' is now your current 'spec' which is { "id": ..., "title": ..., etc. } 
     specsHtml += '<span class=\"'; 
     if (this.title.includes('Length')) 
      specsHtml += 'length'; 
     else 
      specsHtml += 'width'; 

     specsHtml = specsHtml + '\">' + this.value + '</span>'; 
     }); 
     specsHtml += '</div>'; 
     } 
    container.find('.product-bottom .measurements span').html(specsHtml); 
}); 
관련 문제