Page 1 of 1

[HELP] Count Array

Posted: Sun Dec 20, 2015 2:53 am
by Grandong
HI...
i have json like this:

Code: Select all

{  
   "depart":[  
      {  
         "segment":[  
            [  
               {  
                  "airline_code":"SJ",
                  "fno":"SJ598",
                  "class":"X",
                  "seat":null,
                  "from":"CGK",
                  "to":"TTE",
                  "std":"00:05",
                  "sta":"07:05",
                  "price":"1,211,000",
                  "order_key":":-:S:CGK:AMQ:U2s5VlVrNUZXUT09"
               }
            ],
            [  
               {  
                  "airline_code":"SJ",
                  "fno":"SJ694",
                  "class":"X",
                  "seat":null,
                  "from":"TTE",
                  "to":"AMQ",
                  "std":"12:45",
                  "sta":"13:35",
                  "price":"379,000",
                  "order_key":":-:S:CGK:AMQ:U2s5VlVrNUZXUT09"
               }
            ]
         ]
      },
how to count depart[key]['segment'] ?
i try using

Code: Select all

depart[key]['segment'].length
, but now work...

thx...

Re: [HELP] Count Array

Posted: Sun Dec 20, 2015 12:11 pm
by Christopher

Code: Select all

{  
   "depart":[  
      {  
         "segment":[  
            [  
               {  
                  "airline_code":"SJ",
If you follow this JSON down you have:

1. An object with property "depart"
2. "depart" is an array
3. The first element in array "depart" is an object
4. The object has a property "segment"
5. "segment" is an array
6. The first element of array "segment" is an array
7. The first element in that array is an object with many properties "airline_code",...

So maybe something like?

Code: Select all

myobj.depart[0].segment[0][0].airline_code
If you are generating this JSON, then I would recommend simplifying it. If you have no control over it, then you just have to deal with it.

Re: [HELP] Count Array

Posted: Sun Dec 20, 2015 1:38 pm
by Grandong
Thx very much Christopher... you save my day...

now, have a new problem in js
this my problem:
i have table id from ajax response, i try use this table id for make Jquery datatable. but, this doesn't work..
Please.. help me again :D

thx vey much Christopher

Re: [HELP] Count Array

Posted: Sun Dec 20, 2015 10:52 pm
by Christopher
Grandong wrote:
i have table id from ajax response, i try use this table id for make Jquery datatable. but, this doesn't work..
Please start a new thread for this new question. Include the javascript and the data.