JSON response data length getting undefined error in Ajax?
11,966
Solution 1
Try : Object.keys(res.data[0].language).length
Live example :
var res = {
"data" : [
{
"language" : { "107":"english", "142":"hindi", "143" : "indonesian"}
}
]
}
alert("There are " + Object.keys(res.data[0].language).length + " languages." )
Solution 2
Use Object.keys().forEach();
.count your json length.....CHeck to click here....
var json={"data":[{"language":{"190":"english","191":"gujarati"}}]};
var length=0;
Object.keys(json.data[0].language).forEach(function(key) {
length++;
});
alert(length);
Solution 3
var res={"data":[{"language":{"190":"english","191":"gujarati"}}]};
console.log( Object.keys(res['data'][0].language).length);
Solution 4
You misspelt language
as languge
, which is why it is undefined
.
Even if you had not, it is an object not an array, so it doesn't have a length
property. You'll need to count the keys to find out how many there are.
Author by
Developer
Updated on September 06, 2022Comments
-
Developer over 1 year
I want to get the length of
language
in JavaScript alert box -See screenshot-
Ajax Response -
My Ajax code -
function search_menu(){ $.ajax({ type: 'post', url: rootUrl() + "rest_apis/search_menu.json", cache: false, success: function(res){ //alert(data.data[0].language[142]); var len = res.data[0].language.length; alert(len); //Showing undefined }, contentType: 'application/json', dataType: 'json' }); }
I am just alerting
alert(lang)
its showing undefined. Actually in language having 36 record. why its showingundefined
? -
Quentin over 9 yearsAfter your edit, you should not get the result you describe. You should get
[Object object]
alerted. -
Developer over 9 yearsGreat answer. Thanks :)
-
Quentin over 9 yearsAfter your second edit: See the second paragraph of my answer.
-
Jeremy Thille over 9 yearsGreat, that's exactly my answer :)
-
Developer over 9 yearsSorry there are typing mistake.
-
Sadikhasan over 9 yearsDownvoter comment will help me to improve my answer.
-
Jeremy Thille over 9 yearsI don't understand why people get sanctioned by downvotes so quickly, when they're just trying to help. My answer, although correct and later validated by the OP, was immediately downvoted twice. Those downvotes are just unfair.
-
Jeremy Thille over 9 yearsWhat's the "use_native_lan" variable for? Why don't you just replace "forEach(function(key) { length++;});" with just ".length" ? It would be cleaner, faster, and...well, that would be my answer :)
-
hari over 9 years@JeremyThille, Sorry for that(now I changed),offcourse,it better solution,This is an another solution to find length.
-
Jeremy Thille over 9 yearsSure, it works; but it's just slower, more code, more lines, more variables, more complicated. In one word it's just less elegant. So I was wondering about its interest?
-
hari over 9 years@JeremyThille,verify my code..I am using
Native Javascript
.Object.keys().length
internally counting length(it means it use for loop). butObject.keys().forEach(function(){ //do your stuff })
. you can add your own stuff inside function.so,I think both is doing same job. -
eriknoyes over 8 yearsJust a bit perplexed as to why the .length property works on another page, while only your answer works on a particular one of mine. Anyway, thanks!