Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys i would like to ask how do you count the size of an array? i have an array that gets its value from the json_encode value passed by my php file... here is the format of my php return json data

<?php
$idArr= array();
while($row= mysql_fetch_array($outputArrayResult)){			
  $idArr["" . $row['id']] = false;
}
			

echo json_encode(array("total_pages" => $num_pages,"ids" => $idArr));
?>

which returns in this format
/* firebug output */

{
"total_pages":12,
"ids":{"1":false,"2":false,"3":false,"4":false,"5":false,"6":false,"7":false,"8":false,"9":false,"10":false,"11":false,"12":false}
}

now in my javascript i handle the ajax data returned using this code

/* ajax request success handler */
success: function(data){	
  var obj = jQuery.parseJSON(data);
  var ids= obj.ids;
  alert("this is " + ids.length + " -- " + ids["2"]);
}


as you can see im retrieving the data using jQuery.parseJSON(data) and pass it to my variable ids. now when i try to alert the value it gives me this output

/* alert message */
this is undefined -- false

so it means it successfully retrieved the value of ids["2"] but i cant get the length of the array to output properly... how do you do this?
Posted

Just to clarify the correct answer by Tanveer A:

You need to use the property length; please see: http://www.w3schools.com/jsref/jsref_length_array.asp[^].

This will only work for "traditional" arrays indexed by integers.

For a general case of an object, one should count its "own" members:

JavaScript
count = 0;
for(index in x)
    if (x.hasOwnProperty(index))
        ++count;



All JavaScript objects (not counting the primitive objects like numeric of course) are associative arrays. Please see the section "object based" here:
http://en.wikipedia.org/wiki/JavaScript#Dynamic[^].

See also:
http://en.wikipedia.org/wiki/Associative_array[^].

—SA
 
Share this answer
 
v2
The problem is that ids is not an array. Javascript does not have associative arrays, just anonymous objects.

So you'll have to count the members if you really need to know how many there are:
JavaScript
var x, count = 0;
for(x in ids)
  ++count;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Feb-12 14:44pm    
In fact, all JavaScript objects are associative arrays.
Please see the section "object based" here:
http://en.wikipedia.org/wiki/JavaScript#Dynamic

See also: http://www.w3schools.com/jsref/jsref_length_array.asp

So, I can only vote a big 1. Please double check before confusing people with something which is not true.
--SA
Graham Breach 22-Feb-12 16:55pm    
OK, I stand corrected on the objects not being associative arrays part.

But they still don't support .length - and I did check using the console in Chrome and FireFox:

> var x = [1,2,3]; x.length;
3
> var x = {a:1,b:2,c:3}; x.length;
undefined
Sergey Alexandrovich Kryukov 23-Feb-12 1:15am    
Oh, you are right!

count = 0;
for(index in x)
if (x.hasOwnProperty(index))
++count;

will return the length (number of members, actually). This is still an associative array, indexed by 'a', 'b', 'c'.
--SA
Sergey Alexandrovich Kryukov 23-Feb-12 1:16am    
Therefore, I up-voted the answer. Sorry that I missed the point if first place.
--SA
Madzmar25 23-Feb-12 0:43am    
Yes that was my last resort.... i cant figure out how to get the length so i had to iterate every element in my array just to get the size... thanks again for the answer
to get your array length arrayname.length
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Feb-12 14:50pm    
Of course! What else should it be. As you were the only one who answered without telling a lie (which is amazing, considering such a simple problem :-), I voted 5.
I added some more information in my answer, credited your answer.
--SA
Did you try Googling "Javascript array length"? I got this as the very first result: http://www.w3schools.com/jsref/jsref_length_array.asp[^]
 
Share this answer
 
Comments
Madzmar25 22-Feb-12 0:16am    
yes i just did that... as you can see on my first post
alert("this is " + ids.length + " -- " + ids["2"]); where ids = array
krumia 22-Feb-12 0:18am    
Oops so sorry. Can't figure out how I missed that :D
krumia 22-Feb-12 0:33am    
See if this is a similar kind of problem: http://www.sitepoint.com/forums/showthread.php?567312-JSON-length

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900