JSON数组.length属性不起作用

时间:2015-09-02 16:43:46

标签: javascript arrays json

我有以下JSON数组:

[  
   {  
      "orderStatus":"S",
      "x_ExtnIsModifiable":"N",
      "grandTotal":"24",
      "orderId":"",
      "grandTotalCurrency":"USD",
      "placedDate":"2015-05-11T17:56:27.406Z",
      "x_ExtnIsModifiableUntil":"2012-07-24 23:59:00.0",
      "externalOrderID":"29001e"
   },
   {  
      "orderStatus":"S",
      "x_ExtnIsModifiable":"N",
      "grandTotal":"23",
      "orderId":"",
      "grandTotalCurrency":"USD",
      "placedDate":"2015-04-11T17:56:27.406Z",
      "x_ExtnIsModifiableUntil":"2012-07-24 23:59:00.0",
      "externalOrderID":"29001d"
   },
   {  
      "orderStatus":"S",
      "x_ExtnIsModifiable":"N",
      "grandTotal":"22",
      "orderId":"",
      "grandTotalCurrency":"USD",
      "placedDate":"2015-03-11T17:56:27.406Z",
      "x_ExtnIsModifiableUntil":"2012-07-24 23:59:00.0",
      "externalOrderID":"29001c"
   }
]

此数组名为“OrderStatusArray”。我试图获得数组的长度而不用硬编码(出于显而易见的原因),但我无法弄清楚如何做到这一点。我已经在SO上发了十几篇文章,说我应该能够使用OrderStatusArray.length来获取数组的长度,但这对我来说不起作用。

如何获得此数组的长度?

2 个答案:

答案 0 :(得分:1)

看到这个小提琴:https://jsfiddle.net/x983vvr6/2/

基本上,假设您已正确设置对象,则只需调用

即可
OrderStatusArray.length

它将显示数组的长度。

你期待多长时间? 3是正确的答案和这个小提琴的答案。

完整代码:

var OrderStatusArray = [{
"orderStatus": "S",
    "x_ExtnIsModifiable": "N",
    "grandTotal": "24",
    "orderId": "",
    "grandTotalCurrency": "USD",
    "placedDate": "2015-05-11T17:56:27.406Z",
    "x_ExtnIsModifiableUntil": "2012-07-24 23:59:00.0",
    "externalOrderID": "29001e"
  }, {
"orderStatus": "S",
    "x_ExtnIsModifiable": "N",
    "grandTotal": "23",
    "orderId": "",
    "grandTotalCurrency": "USD",
    "placedDate": "2015-04-11T17:56:27.406Z",
    "x_ExtnIsModifiableUntil": "2012-07-24 23:59:00.0",
    "externalOrderID": "29001d"
   }, {
"orderStatus": "S",
    "x_ExtnIsModifiable": "N",
    "grandTotal": "22",
    "orderId": "",
    "grandTotalCurrency": "USD",
    "placedDate": "2015-03-11T17:56:27.406Z",
    "x_ExtnIsModifiableUntil": "2012-07-24 23:59:00.0",
    "externalOrderID": "29001c"
     }];


console.log("Your order length array is: " + OrderStatusArray.length);

答案 1 :(得分:-1)

var data = [  
   {  
      "orderStatus":"S",
      "x_ExtnIsModifiable":"N",
      "grandTotal":"24",
      "orderId":"",
      "grandTotalCurrency":"USD",
      "placedDate":"2015-05-11T17:56:27.406Z",
      "x_ExtnIsModifiableUntil":"2012-07-24 23:59:00.0",
      "externalOrderID":"29001e"
   },
   {  
      "orderStatus":"S",
      "x_ExtnIsModifiable":"N",
      "grandTotal":"23",
      "orderId":"",
      "grandTotalCurrency":"USD",
      "placedDate":"2015-04-11T17:56:27.406Z",
      "x_ExtnIsModifiableUntil":"2012-07-24 23:59:00.0",
      "externalOrderID":"29001d"
   },
   {  
      "orderStatus":"S",
      "x_ExtnIsModifiable":"N",
      "grandTotal":"22",
      "orderId":"",
      "grandTotalCurrency":"USD",
      "placedDate":"2015-03-11T17:56:27.406Z",
      "x_ExtnIsModifiableUntil":"2012-07-24 23:59:00.0",
      "externalOrderID":"29001c"
   }
];
console.log( data.length );
你想要这个吗?