无法提取[对象,对象]值

时间:2020-06-04 12:52:36

标签: javascript jquery ajax

所以我试图从一个对象中获取一个值。一种产品如下所示:

{
'amount': '29290',
'orders': '2',
'pricePerUnit': '15.5'
}

我需要pricePerUnit,我尝试做过Object.values(product_buyPrice),但在我的网站上仍然显示[Object, object],我检查了其他线程并尝试了该线程上的内容,但没有用。如何从pricePerUnitproduct_buyPrice中提取product_sellPrice

这是我的JavaScript代码:

<script type=text/javascript>
        $SCRIPT_ROOT = {{ request.script_root | tojson | safe }};
        (function () {
            $.getJSON($SCRIPT_ROOT + "/bprices",
            function(data) {
                var products = data.products;
                var table_body = document.createElement("tbody");
                $.each(products, function(index, product){
                    var product_name = product.id.toString();
                    var product_buyPrice = product.buy_price;
                    var product_sellPrice = product.sell_price;
                    var row = table_body.insertRow();
                    var name_cell = row.insertCell();
                    //console.log(product_buyPrice)
                    name_cell.appendChild(document.createTextNode(product_name));
                    var quantity_cell = row.insertCell();
                    quantity_cell.appendChild(document.createTextNode(Object.values(product_buyPrice)));
                    var price_cell = row.insertCell();
                    price_cell.appendChild(document.createTextNode(product_sellPrice));
                })
                $("#products_table tbody").remove();
                $("#products_table").append(table_body);
            }
            );
            setTimeout(arguments.callee, 10000);
        })();
    </script>

1 个答案:

答案 0 :(得分:-1)

在下面的代码段中,我使用了$.each循环来遍历对象数组并提取所需的值。即:

//your response
var products = [{
    "id": "Abc",
    "sell_price": [{
      "amount": '727',
      "pricePerUnit": '3.7',
      "orders": '1'
    }],
    "buy_price": [{
      'amount': '29290',
      'orders': '2',
      'pricePerUnit': '15.5'
    }],
  },
  {
    "id": "Xyz",
    "sell_price": [{
      "amount": '31392',
      "pricePerUnit": '13.3',
      "orders": '4'
    }],
    "buy_price": [{
      'amount': '29290',
      'orders': '2',
      'pricePerUnit': '15.5'
    }],
  }
];

//var products = data.products;
var table_body = document.createElement("tbody");
$.each(products, function(index, product) {
  var product_name = product.id.toString();
  //looping through buy_price array
  $.each(product.buy_price, function(index, product1) {
    var product_buyPrice = product1.pricePerUnit; //getting value
    //looping through sell_price array
    $.each(product.sell_price, function(index, product2) {
      var product_sellPrice = product2.pricePerUnit; //getting value
      var row = table_body.insertRow();
      var name_cell = row.insertCell();
      //console.log(product_buyPrice)
      name_cell.appendChild(document.createTextNode(product_name));
      var quantity_cell = row.insertCell();
      quantity_cell.appendChild(document.createTextNode(product_buyPrice));
      var price_cell = row.insertCell();
      price_cell.appendChild(document.createTextNode(product_sellPrice));
    });
  });
})
$("#products_table tbody").remove();
$("#products_table").append(table_body);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="products_table" border='1'>

</table>