Find length of Object using Jquery.

Posted by Nratz83 under jQuery on 10/9/2012 | Points: 10 | Views : 7043 | Status : [Member] | Replies : 2
Hi,

I would like to know, how can we get the length of an Object using jquery. I have tried using object.length property, but it gives me result as undefined.
Below is my code where i have been able to get the keys and their values, but what i am looking is to get the length of my object.

jQuery.each(c, function(name, value) { 
alert(name + ": " + value);
});


Any help is appreciated.

Thanks.




Responses

Posted by: Kanje_Ravi on: 10/9/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Nratz83 ,

As per your query i understand that you are looking to for loop an array or collection..please find the below block of code..may it help you..
Put it in your script block and check..


$(document).ready(function () //this function is needed to display on page load..
{
var test = new Array(); //declare an empty array
var test = new Array("Ravi", "Ramesh", "Rockesh"); //assign values to it
$.each(test, function (i) //for loop an array here i=index of an array
{
alert(test[i]);
})
});


Thanks & Regards,
Ravindra M
(Success Always Depends on Dedication & Commitment)

Nratz83, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Devi0074u on: 3/12/2013 [Member] Starter | Points: 25

Up
0
Down
You can use length to get the length of any array.

e.g: new Array("A", "B", "C").length // will give you 3.

As you said object, which kind of object it is.
Is it a jSON string that you are trying to iterate?
In that case, you can use eval() / JSONStringify() to convert the string to jSON and then you can use length property .

Hope it will help you :)

Nratz83, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response