【问题标题】:How do I access this javascript object?我如何访问这个 javascript 对象?
【发布时间】:2014-06-19 01:59:59
【问题描述】:
{"profit_center" : 

{"branches":

[

{"branch":      {"work_order":"1","cutover":"1","site_survey":"1","branch_number":"3310","quote":"1","configuration":"1","purchase_order":"1","hardware_swap":"1"}},

{"branch":{"work_order":"1","cutover":"1","site_survey":"1","branch_number":"3311","quote":"1","configuration":"1","purchase_order":"1","hardware_swap":"1"}},

{"branch":{"work_order":"1","cutover":"0","site_survey":"1","branch_number":"3312","quote":"1","configuration":"1","purchase_order":"1","hardware_swap":"1"}},

{"branch":{"work_order":"1","cutover":"1","site_survey":"1","branch_number":"3313","quote":"1","configuration":"1","purchase_order":"1","hardware_swap":"1"}},

{"branch":{"work_order":"1","cutover":"0","site_survey":"1","branch_number":"3314","quote":"1","configuration":"1","purchase_order":"1","hardware_swap":"1"}},

{"branch":{"work_order":"1","cutover":"1","site_survey":"1","branch_number":"3315","quote":"1","configuration":"1","purchase_order":"1","hardware_swap":"1"}}

],


"profit_center_name":"Alabama"}}

我尝试通过这个在 ajax 中访问它,

data.profit_center //data here is the ajax variable e.g. function(data)

或通过此data["profit_center"]

但没有运气

如何正确访问此 javascript 对象。 ?

顺便说一下,上面的代码来自console.log(data)

编辑:

console.log(data.profit_center)console.log(data["profit_center"]) 的结果未定义

【问题讨论】:

  • 您需要准确访问什么?
  • 一切,访问那里的任何东西,以便我了解如何
  • 确保数据不是null。当您尝试执行该 javascript 时,控制台是否会给出任何消息?
  • 我发布的代码来自 console.log(data)。所以它不为空

标签: javascript object hash associative


【解决方案1】:

你可以把你的data放在一个变量中

var json = data

你可以访问profit_centerlike

alert(json.profit_center);

alert(json.profit_center.profit_center_name); //Alabama

for(var i =0 ;i<json.profit_center.branches.length;i++){ alert(json.profit_center.branches[i]); }

【讨论】:

    【解决方案2】:

    好的,我找到了它未定义的原因,它是一个 json 对象,所以我需要先解析它,然后才能像 javascript 对象一样访问它。

    var json = JSON.parse(data);
    

    那就这样吧。

    【讨论】:

      【解决方案3】:

      如果您还没有这样做,请先解析您的数据。

      例如,您可以像这样访问每个branch_number

      var branches = data.profit_center.branches;
      
      for (var i = 0, l = branches.length; i < l; i++) {
        console.log(branches[i].branch.branch_number);
      }
      

      总之,profit_center 是一个对象,branches 是一个对象数组。数组中的每个元素都包含一个包含许多键的分支对象。循环 branches 数组,并为每个元素使用键名访问内部的分支对象以获取值。

      通过访问profit_center 对象上的profit_center_name 键可以找到利润中心名称:

      console.log(data.profit_center.profit_center_name); // Alabama
      

      您甚至可以使用新的函数式数组方法来查询数据并仅提取您需要的那些分支。这里我使用filter 来拉取那些purchase_order 等于2 的对象。请注意,JSON 中的数值是字符串,而不是整数。

      var purchaseOrder2 = branches.filter(function (el) {
        return el.branch.purchase_order === '2';
      });
      

      DEMO for the three examples

      【讨论】:

        猜你喜欢
        • 2012-03-13
        • 1970-01-01
        • 2019-04-11
        • 1970-01-01
        • 2021-07-05
        • 2018-10-23
        • 2011-08-22
        • 2021-05-13
        • 1970-01-01
        相关资源
        最近更新 更多