I want to find the length of this JSON object so any one tell me how can i get length of the JSON object...means i want to know how many data this json object contain.
var ddData = [{ "01":"United States",
"02":"United Kingdom",
"03":"Aruba",
"04":"United Kingdom",
"05":"Aruba",
"06":"Bahrain",
"07":"United Kingdom",
"08":"Algeria",
"09":"Andorra",
"10":"American Samoa",
"11":"United States"
}]
I want to find the length of this JSON object so any one tell me how can i get length of the JSON object...means i want to know how many data this json object contain.
var ddData = [{ "01":"United States",
"02":"United Kingdom",
"03":"Aruba",
"04":"United Kingdom",
"05":"Aruba",
"06":"Bahrain",
"07":"United Kingdom",
"08":"Algeria",
"09":"Andorra",
"10":"American Samoa",
"11":"United States"
}]
Share
Improve this question
edited Mar 2, 2016 at 2:50
computingfreak
5,0991 gold badge37 silver badges52 bronze badges
asked Jan 11, 2013 at 5:44
codercoder
1,8923 gold badges22 silver badges32 bronze badges
0
6 Answers
Reset to default 12You could use Object.keys
method to get the object keys.
Since ddData is an array
, and the first index contains an object, you could do this :
Object.keys(ddData[0]).length;
However, Object.keys
does not work on old browsers.
For that, you could iterate through the object and increment a variable to get the length of that object.
var count = 0;
for(var i in ddData[0]) {
count++;
}
You can use the Object.key
s that will return the length of specified collection.JS Fiddle.
(Browser support from here) (Doc on Object.keys here, includes method you can add to non-ECMA5 browsers)
you can simply do this instead of calling functions
var key, count = 0;
for(key in ddData[0]) {
count++;
}
console.log(count + " is size of the json");
var length=0;
for(attr in ddData[0]){
length++;
}
var json = JSON.parse(ddData);
for(i=0;i<json.length;i++)
{
alert(json[i].length);
}
Now i got the answer....
var Data = { "01":"United States",
"02":"United Kingdom",
"03":"Aruba",
"04":"United Kingdom",
"05":"Aruba",
"06":"Bahrain",
"07":"United Kingdom",
"08":"Algeria",
"09":"Andorra",
"10":"American Samoa",
"11":"United States"
}
var count = 0;
for(x in Data){
count++;
}
console.log('cnt',count)