[HELP] Count Array

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

[HELP] Count Array

Post 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...
Newbie The Passion for Learning
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: [HELP] Count Array

Post 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.
(#10850)
User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

Re: [HELP] Count Array

Post 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
Newbie The Passion for Learning
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: [HELP] Count Array

Post 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.
(#10850)
Post Reply