【问题标题】:How to access data in complex structure?如何访问复杂结构的数据?
【发布时间】:2016-11-08 15:17:58
【问题描述】:

我正在尝试整理我网站的愿望清单功能,为此我需要从另一个数组中的一个数组中调用一个值,我需要获取底层 products 对象值,具体取决于哪个第 4 级 @需要 987654323@ 对象(取决于第 4 级 id(本例中为 44、9、8、7、6、475)):

var jsonProductData = {
    "attributes" : {
        "133": {
            "id":"133",
            "code":"size",
            "label":"Size",
            "options": [
                {"id":"44","label":"XS","price":"0","oldPrice":"0","cssclass":"","products":["11921"]},
                {"id":"9","label":"S","price":"0","oldPrice":"0","cssclass":"","products":["11922"]},
                {"id":"8","label":"M","price":"0","oldPrice":"0","cssclass":"","products":["11923"]},
                {"id":"7","label":"L","price":"0","oldPrice":"0","cssclass":"","products":["11924"]},
                {"id":"6","label":"XL","price":"0","oldPrice":"0","cssclass":"","products":["11925"]},
                {"id":"475","label":"XXL","price":"0","oldPrice":"0","cssclass":"","products":["11926"]}
            ]
        }
    },
    "template" : "\u00a3#{price}",
    "basePrice" : "187",
    "oldPrice" : "299.99",
    "productId" : "11950",
    "chooseText" : "Select Size...",
    "taxConfig" : {
        "includeTax" : false,
        "showIncludeTax" : true,
        "showBothPrices" : false,
        "defaultTax" : 0,
        "currentTax" : 0,
        "inclTaxTitle" : "Inc VAT"
    }
};

虽然可能有多个二级“属性”对象,但到目前为止我有:

for (var key in jsonProductData['attributes']) {
    $j(jsonProductData.attributes[key].options.select(.id==7)
}

我知道这不是特别远,尽管我完全不知道如何克服这一点,因为我需要使用的 id 是对象中的一个值,这似乎不适用于这个 select 函数.

谁能帮忙?

【问题讨论】:

  • $j(jsonProductData.attributes[key].options.select(.id==7) 中某处缺少)。另外,$j 是什么?图书馆?
  • 当您到达这一点时,它不再是 JSON:它只是一个对象。因此,访问数据的方式与访问任何其他对象结构的方式相同。如果您有一个对象数组(例如,“选项”),您将如何获得所需的元素? Find object by id in an array of JavaScript objects,也许吧。
  • 嘿乔丹,$j 出于某种原因是我们网站的 jquery。该功能还没有完成,所以我试图找出我需要添加的其他内容。
  • 嘿,迈克,不幸的是,一般来说,javascript 远非我的能力,我会看看那个链接,看看我能不能弄明白
  • 对不起,你能举一个输入的例子吗?输出应该是什么?我有点迷失了你想在这里做什么。

标签: javascript jquery arrays multidimensional-array


【解决方案1】:

这将从options 数组中为每个项目获取id

var jsonProductData = {
    "attributes" : {
        "133": {
            "id":"133",
            "code":"size",
            "label":"Size",
            "options": [
                {"id":"44","label":"XS","price":"0","oldPrice":"0","cssclass":"","products":["11921"]},
                {"id":"9","label":"S","price":"0","oldPrice":"0","cssclass":"","products":["11922"]},
                {"id":"8","label":"M","price":"0","oldPrice":"0","cssclass":"","products":["11923"]},
                {"id":"7","label":"L","price":"0","oldPrice":"0","cssclass":"","products":["11924"]},
                {"id":"6","label":"XL","price":"0","oldPrice":"0","cssclass":"","products":["11925"]},
                {"id":"475","label":"XXL","price":"0","oldPrice":"0","cssclass":"","products":["11926"]}
            ]
        }
    },
    "template" : "\u00a3#{price}",
    "basePrice" : "187",
    "oldPrice" : "299.99",
    "productId" : "11950",
    "chooseText" : "Select Size...",
    "taxConfig" : { "includeTax":false,"showIncludeTax":true,"showBothPrices":false,"defaultTax":0,"currentTax":0,"inclTaxTitle":"Inc VAT" }
};
var idToLookFor = 7;
for (var key in jsonProductData.attributes) {
  var options = jsonProductData.attributes[key].options;
  for (var i=0; i < options.length; i++) {
    console.log('options[' + i + '].id = ' + options[i].id);
    var idAsInteger = parseInt(options[i].id);
    if (idToLookFor === idAsInteger) {
      var products = options[i].products;
      for (var j=0; j < products.length; j++) {
        console.log('   --> products[' + j + '] = ' + products[j]);
      }
    }
  }
}

【讨论】:

  • 嘿彼得,感谢您的回复,是的,但是我如何获取给定 ID 的“产品”对象(更重要的是相关的 5 位数字)?
  • 嘿彼得,这很有效,我肯定需要研究它,看看我哪里出错了,因为我可以发誓我已经尝试过类似 jsonProductData.attributes[key].options[0 ].products 并没有返回任何东西!再次感谢
  • 嘿彼得,还有一个简单的问题,我在 for 部分开始后使用“var idToLookFor = $j("#attribute"+ key).val()" 来获取选择要搜索的属性“值”。控制台日志显示每个值在开始时都是正确的,但现在控制台日志 options[i].products 返回 undefined - 如果我手动将 idToLookFor 分配给相同的值,它会返回正确的值,会出现什么问题?
  • 调用 val() 返回一个字符串,然后 === 会产生 false。确保 === 两侧使用相同的类型。如有必要,使用 parseInt()。
【解决方案2】:

您可以获取这些嵌套对象的列表,然后找到具有特定 id 的对象,如下所示:

var jsonProductData={"attributes":{"133":{"id":"133","code":"size","label":"Size","options":[{"id":"44","label":"XS","price":"0","oldPrice":"0","cssclass":"","products":["11921"]},{"id":"9","label":"S","price":"0","oldPrice":"0","cssclass":"","products":["11922"]},{"id":"8","label":"M","price":"0","oldPrice":"0","cssclass":"","products":["11923"]},{"id":"7","label":"L","price":"0","oldPrice":"0","cssclass":"","products":["11924"]},{"id":"6","label":"XL","price":"0","oldPrice":"0","cssclass":"","products":["11925"]},{"id":"475","label":"XXL","price":"0","oldPrice":"0","cssclass":"","products":["11926"]}]}},"template":"\u00a3#{price}","basePrice":"187","oldPrice":"299.99","productId":"11950","chooseText":"Select Size...","taxConfig":{"includeTax":false,"showIncludeTax":true,"showBothPrices":false,"defaultTax":0,"currentTax":0,"inclTaxTitle":"Inc VAT"}};

var arr = Object.keys(jsonProductData['attributes']).reduce( function(acc, key, i, attr) {
    return acc.concat(jsonProductData['attributes'][key].options);
}, []);

// find obj with number 7:
var obj = arr.find(function (o) { return o.id == 7 });

// print it
console.log(obj);

// get product reference:
console.log('found product:', obj.products[0]);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

    猜你喜欢
    • 2015-12-14
    • 2012-05-12
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多