在对象

时间:2016-10-21 13:59:29

标签: jquery json

首先我还在学习所以请温柔:) 我有一个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

现在我只想从规范“length”和“height”中获取两个value属性。首先,我必须查看这两个名称是否存在并匹配“length”和“height”,如果是,请抓住他们的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);
 });

我完全陷入了困境。我尝试过这样的事情:

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

任何帮助都非常感谢:)

2 个答案:

答案 0 :(得分:1)

$.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);
 });

答案 1 :(得分: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>