Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a problem with getting the length of an array, when it is not initialized in the format: var variable = new Array();

Here is my code:

var inx;
var qns;
var qis;
var ncs;
var nzs;
var tps;

function display_question()
{

   $( "#question_no" ).text( qns[ inx ] );
   $( "#question_nc" ).text( ncs[ inx ] );
   $( "#question_nz" ).text( nzs[ inx ] );
   $( "#the_question" ).hide();
   $( "#the_question" ).text( tps[ inx ] );
   $( "#the_question" ).fadeIn( 500 );

}

function next_question()
{

   var arr_len = qns.length;

   if( inx < arr_len )
   {

      inx++;
      display_question();

   }

}

function prev_question()
{

   if( inx > 0 )
   {

      inx--;
      display_question();

   }

}

function get_questions()
{

   var url = "php/pytania.php"; 

   $.ajax(
   {

      cache: false,
      async: false,
      type: "GET",
      dataType: "text",
      url: url,
      success: function( response ) 
      { 

         data = jQuery.parseJSON( response );

         inx = 0;
         qns = data.qns;
         qis = data.qis;
         ncs = data.ncs;
         nzs = data.nzs;
         tps = data.tps;
         display_question();

      }

   } );

}

The problem is that when I try to return the length of qns like so: qns.length, it doesn't return anything. I suspect that this may be because when i read in the array from the JSON response, it is not as an array object. Is there any way to fix this? I'd appreciate any help! :)

share|improve this question
    
Could you please show the JSON response? Most likely it does not work because it is not an array but an object. Btw. arrays don't have to be initialized this way. JSON arrays and objects are parsed into JS arrays and objects. There is not difference. –  Felix Kling May 12 '11 at 13:11
    
yup when i output qns with an alert, here is what i get: [object Object] - is there any way to convert it to an array without having to traverse through it? the curious thing is that when i output qns[ index] - it displays the content correctly... –  Piotr May 12 '11 at 13:15
    
So it is especially about data.qns? If it is not array, you have to loop over it and count the elements. But if you have continuous numeric keys, then it would be better to fix the JSON to create an array. So what does data.qns contain and how are you creating the JSON? –  Felix Kling May 12 '11 at 13:19
    
I am creating the JSON with a back-end PHP script, like so: $json_array = array( "qns" => $qns, "qis" => $qis, "ncs" => $ncs, "nzs" => $nzs, "tps" => $tps ); echo json_encode( $json_array, JSON_FORCE_OBJECT ); –  Piotr May 12 '11 at 13:21
    
Is $qns an array? If so, just use echo json_encode($json_array); to let PHP create a JSON array. No need for objects here. Then qns.length will work as expected. Associative are converted to objects anyway. But if you use JSON_FORCE_OBJECT, so will numerical arrays. –  Felix Kling May 12 '11 at 13:22
show 4 more comments

1 Answer

up vote 0 down vote accepted

There is no difference between arrays/objects you retrieve via JSON and those created in JavaScript. The JSON objects and arrays are parsed into JavaScript ones.
And there is actully no need to call new Array() ever.

It seems data.qnr is an object not an array. You would have to loop over the objects and count the elements yourself.

However as it seems that the object is continuous numerical keys, it would be better to create the correct output (array) from the beginning.

In your comments you wrote you use json_encode( $json_array, JSON_FORCE_OBJECT ) to create the JSON output. This will convert every array, even numerical ones, into objects.

Instead, just use json_encode($json_array). By default PHP will convert associative arrays to objects in JSON and numerical arrays to ... well, arrays ;)

If you do this, data.qnr will work as expected in JavaScript.


Also note, instead of dataType: 'text', you can set the it to json and jQuery will parse the response automatically for you (so no need to call jQuery.parseJSON).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.