-2

我有以下 json 对象。当用户从框中选择一种颜色时select,我想加载相应的qty. 您将如何选择颜色等于的数量$('#colorselector').val()

{
    "products": [{
        color: "yellow",
        qty: 22
    }, {
        color: "red",
        qty: 45
    }, {
        color: "blue",
        qty: 3
    }]
}
4

3 回答 3

1
jsonObj.products.forEach(function(el) {
    if(el.color == $('#colorselector').val()) {
        console.log("Qty: " + el.qty);
    }
}
于 2013-10-27T07:34:42.860 回答
0

这取决于您的需求,例如您可以执行以下操作:

{
    "products": {
      "yellow":22,
      "red":45,
      "blue": 3
    }
}

并通过

qty = obj.products[$('#colorselector').val()]

但正如我所说,这取决于你将如何使用它

于 2013-10-27T07:33:07.357 回答
0
var data = {
    "products": [{
        color: "yellow",
        qty: 22
    }, {
        color: "red",
        qty: 45
    }, {
        color: "blue",
        qty: 3
    }]
};

console.log(data.products[0].qty);
于 2013-10-27T07:39:39.737 回答