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);

JS Fiddle

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.

Share:
11,966
Developer
Author by

Developer

Updated on September 06, 2022

Comments

  • Developer
    Developer over 1 year

    I want to get the length of language in JavaScript alert box -

    See screenshot-

    Ajax Response -

    enter image description here

    enter image description here

    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 showing undefined?

  • Quentin
    Quentin over 9 years
    After your edit, you should not get the result you describe. You should get [Object object] alerted.
  • Developer
    Developer over 9 years
    Great answer. Thanks :)
  • Quentin
    Quentin over 9 years
    After your second edit: See the second paragraph of my answer.
  • Jeremy Thille
    Jeremy Thille over 9 years
    Great, that's exactly my answer :)
  • Developer
    Developer over 9 years
    Sorry there are typing mistake.
  • Sadikhasan
    Sadikhasan over 9 years
    Downvoter comment will help me to improve my answer.
  • Jeremy Thille
    Jeremy Thille over 9 years
    I 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
    Jeremy Thille over 9 years
    What'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
    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
    Jeremy Thille over 9 years
    Sure, 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
    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). but Object.keys().forEach(function(){ //do your stuff }). you can add your own stuff inside function.so,I think both is doing same job.
  • eriknoyes
    eriknoyes over 8 years
    Just 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!