阵列访问不起作用

时间:2015-10-26 13:09:04

标签: jquery arrays

我有一个如下所示的数据结构:

var merchants = [
    {
    "eway": 
        [{
            "transaction_fee": 0.30,
            "domestic_card_cost": 0.026,
            "plan": "Have a Go Plan",
            "link": eway_link,
            "img": '<img src="/wp-content/uploads/eway.png"/>'
        }]
    },{
    "stripe": 
        [{
            "transaction_fee": 0.30,
            "domestic_card_cost": 0.0175,
            "plan": "Standard Plan",
            "link": stripe_link,
            "img": '<img src="/wp-content/uploads/stripe.png"/>'
        }]
    }
];

我可以访问这样的个人商家:

var selected_merchant = "stripe";

if(value[selected_merchant]) {
    details = value[selected_merchant];
    return false;
}

但是我的下一步失败了。我正在使用这样的数据构建一个表:

function createMerchantTable() {

    var table_row = "";

    jQuery.each(merchants, function(item, value) {

        table_row = "<tr>" +
                        "<td id='" + merchants[item] + "'>" + value[item][img] + "</td>" +
                        "<td>" + value[item][transaction_fee] + "c</td>" +
                        "<td>" + value[item][domestic_card_cost] + "%</td>" +
                    "</tr>";

        jQuery("#merchants tbody").append(table_row);
        table_row = "";

    });

}

如何访问每个商家对象中的值?谢谢!

3 个答案:

答案 0 :(得分:1)

尝试这样:为每个外部对象获取其密钥,然后尝试查找相关属性。

&#13;
&#13;
$(document).ready(function() {
  createMerchantTable();
});

function createMerchantTable() {

  var table_row = "";
  var merchants = [{
    "eway": [{
      "transaction_fee": 0.30,
      "domestic_card_cost": 0.026,
      "plan": "Have a Go Plan",
      "link": 'eway_link',
      "img": '<img src="/wp-content/uploads/eway.png"/>'
    }]
  }, {
    "stripe": [{
      "transaction_fee": 0.30,
      "domestic_card_cost": 0.0175,
      "plan": "Standard Plan",
      "link": 'stripe_link',
      "img": '<img src="/wp-content/uploads/stripe.png"/>'
    }]
  }];
  jQuery.each(merchants, function(index, value) {
    for (key in value) { //since your key is not fixed

      table_row = "<tr>" +
        "<td id='" + key + "'>" + value[key][0]['img'] + "</td>" +
        "<td>" + value[key][0]['transaction_fee'] + "c</td>" +
        "<td>" + value[key][0]['domestic_card_cost'] + "%</td>" +
        "</tr>";
      console.log(table_row);
      jQuery("#merchants tbody").append(table_row);
      tablke_row = "";
    }
  });

}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="merchants">
  <tbody></tbody>
</table>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

根据您的javascript对象结构,您的商家是对象数组。因此,如果您想阅读商家详细信息,您必须这样做:

merchants.eway[0].plan;
merchants.eway[0].link;

您可以使用变量和数组表示法来执行此操作:

var merchant = 'eway';
merchants[merchant][0].link;

答案 2 :(得分:0)

<强> DEMO HERE

我相信你需要使用 3循环来处理expected data format

function createMerchantTable() {
    var table_row = "";
    $.each(merchants, function(key, value) {
        $.each(merchants[key],function(key1,value1){
            $.each(value1,function(key2,value2){
                table_row = "<tr>" +
                        "<td id=''>" + value2["img"] + "</td>" +
                        "<td>" + value2["transaction_fee"]+ "c</td>" +
                        "<td>" + value2["domestic_card_cost"] + "%</td>" +
                    "</tr>";
                $("#merchants tbody").append(table_row);
                tablke_row = "";
            });
        });
    });
}

下图解释了原因?

Why Loop Thrice