在JavaScript中访问数组中的数组 [英] Access array in array in javascript

查看:89
本文介绍了在JavaScript中访问数组中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到JSON答复,如下所示:

I am getting a JSON reply, like following:

[{
  "order_id": "12",
  "customer": "user user",
  "status": "Pending",
  "date_added": "02\/09\/2012",
  "total": "$500.00",
  "action": [{
    "text": "View",
    "href": "http:\/\/localhost\/oc\/admin\/index.php?route=sale\/order\/info&token=92a80574e5fcbf3e2d021404cfaae1a4&order_id=12"
  }]
}]

看一下动作键,它的值还是一个数组.我正在尝试通过以下代码获取操作键值,但对我而言却显示未定义

have a look on action key, it's value is again an array. I am trying to get action key values by following code but it is showing undefined to me

function (data) {
  if (data) {
    for (var i = 0; i < data.length; i++) {
      $('div.dashboard-content table.list tbody tr:first').before(
        '<tr id="' + 
        data[i]['order_id'] + 
        '"><td class="right">' + 
        data[i]['order_id'] + 
        '</td><td class="left">' + 
        data[i]['customer'] + 
        '</td><td class="left">' + 
        data[i]['status'] + 
        '</td><td class="left">' + 
        data[i]['date_added'] + 
        '</td><td class="right">' + 
        data[i]['total'] + 
        '</td><td class="right"> [<a href="' + 
        data[i]['action']['href'] + '">' + 
        data[i]['action']['text'] + 
        '</a>]</td></tr>'
      );
    }
  }
}

有人可以帮我吗? 预先感谢.

Can somebody help me.? Thanks in advance.

推荐答案

就像您说的那样,action是一个数组.因此,您不能使用data[i]['action']['href']访问它.您必须使用下标指示所需数组的位置.例如,要访问第一个职位,您可以使用:

Like you said, action is an array. Thus, you can't access it using data[i]['action']['href']. You have to use a subscript to indicate the position of the array that you want. For example, to access the first position, you'd use:

var href = data[i].action[0].href;
var text = data[i].action[0].text;

这篇关于在JavaScript中访问数组中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆