2016-10-21 9 views
0

私はまだ勉強していますので、穏やかにしてください:) 私はいくつかの特定の要素を選択したいと思うjson応答があります。 オブジェクト内に特定の名前の要素をいくつか取得しようとしていて、定義済みの名前と一致するかどうかをテストします。これらは、「長さ」、「幅」などの特定の名前を持つ製品仕様です。オブジェクト内の名前を選択

これらの特定の名前を見つけ出すのに苦労しています。

それでは、私が持っていることは、このされています。今、私は2つのだけvalue仕様「長さ」から属性と「高さ」をつかむしたい

{ 

"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 

JSONレスポンス。だから最初に私はそれらの2つの名前が存在し、 "長さ"と "高さ"に一致するかどうかを調べなければならず、その場合には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

Thxを!しかし、これはすべてのフィールドで "0"を返し続けます。 /長さ/私のしていることを説明してもいいですか? – Meules

+0

私は間違っていると思う。あなたの 'spec.title'をそのregEx、例えばwidthでテストします。しかし、それはその仕様の '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); 
}); 
関連する問題