2

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?

5
  • 1
    Your code is wrong should be: var len = res.data[0].language; ? Commented Feb 5, 2015 at 11:03
  • Try res[0].language.length; Commented Feb 5, 2015 at 11:07
  • 2
    @Kumar — That definitely won't work. Commented Feb 5, 2015 at 11:07
  • @Kumar your code not working. :( Commented Feb 5, 2015 at 11:11
  • 1
    See my answer below : Object.keys(res.data[0].language).length Commented Feb 5, 2015 at 11:14

4 Answers 4

8

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." )

Sign up to request clarification or add additional context in comments.

1 Comment

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!
3

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

4 Comments

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 :)
@JeremyThille, Sorry for that(now I changed),offcourse,it better solution,This is an another solution to find length.
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?
@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.
2
var res={"data":[{"language":{"190":"english","191":"gujarati"}}]};
console.log( Object.keys(res['data'][0].language).length);

JS Fiddle

3 Comments

Great, that's exactly my answer :)
Downvoter comment will help me to improve my answer.
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.
1

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.

3 Comments

After your edit, you should not get the result you describe. You should get [Object object] alerted.
After your second edit: See the second paragraph of my answer.
Sorry there are typing mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.