7

this is might be a very basic question, but seems like it's not easy to find the answer. I have a Json, more or less is like:

languages = {
"aa":{"iso639-2T":"aar","family":"Afro-Asiatic","labels":{"language_names":["Afar"],"native_names":["Afaraf"]}},
"ab":{"iso639-2T":"abk","family":"Northwest Caucasian","labels":{"language_names":["Abkhaz"],"native_names":["\u0430\u04a7\u0441\u0443\u0430"]}},
"af":{"iso639-2T":"afr","family":"Indo-European","labels":{"language_names":["Afrikaans"],"native_names":["Afrikaans"]}}, etc...etc... }

if you see json above, there's several language object inside languages variable. and each language object has its own name as its identifier ("aa", "ab", "af")

so then, my question is, how to get those identifier ("aa", "ab", "af") if i want to list all of those language in html? eg. if i want to create like a combo box (<option value="aa">Afar</option><option value="ab">Abkhaz</option><option value="af">Afrikaans</option>)

actually what i want to achieve is something like this (in php)

$sampleArray = Array("aa" => "Afar", "ab" => "Abkhaz", "af" => "Afrikaans"); foreach($sampleArray as $id => $value){ /* I can get the id from $id* / }

is there any solution similar like php syntax above for my json in java script?

ps. if you're wondering why i'm not using array - I'm just thinking it will easier to grab certain language object (im just do something like: languages["af"] to get afrikaans language) rather than i should do: loop through the entire language object and check one-by-one if the id is what i want, and then return it. - you can give me another suggestion for this if you guys have a better idea :)

Best Regards,

AnD

1
  • Great this is very fast answer hahaha, thanks guys for helping me out! *see below for the answer :) Commented Oct 19, 2010 at 8:05

5 Answers 5

6

this should do what you want..

to see them

for (var lang in languages)
  alert(lang + ' : ' + languages[lang].labels.language_names[0]);

to put them in the DOM

var $select = $('selector to your select element');
for (var lang in languages)
  $select.append('<option value="'+lang+'">' + languages[lang].labels.language_names[0] + '</option>');

Working code at http://www.jsfiddle.net/EsJAh/

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

2 Comments

alert(lang + ' : ' + languages[lang].labels.language_names[0] ?
@james, nice catch .. missed the array there.. fixed in answer.
3

You can use the "for"

for (var key in languages)
{
 alert(key);
}

Comments

2

Did you try:

languages['af'].labels.language_names[0]

It will return Afrikaans

1 Comment

I think in this example what he doesn't have the 'af' in the first place. That's what he's trying to get.
2
var data = new Array();
for(var lang in languages) {
    data[lang] = languages[lang];
}

3 Comments

Using Array here is wrong. You should use {} aka new Object(), yes it works with Array too, but that's only because you're setting properties on the Array Object, you're in fact not setting anything IN the Array :)
@Ivo - I completely agree with you. It just seems like the OP already has what he's looking for in the languages object. So, I figured I'd give him something else.
What Ivo is saying is.. When you set data["foo"] = "bar", you're not actually adding the key/value "foo" => "bar" to the array. You're actually adding the foo property to the array object, i.e. Array.foo = "bar". While it will work 90% of the time, there are a few gotchas that pop up.
0

If you're using jquery, then you can do this actually:

$.each( languages, function(k, v){
        alert( "Key: " + k + ", Value: " + v );
        // value is your particular language object based on k variable
});

:)

Comments

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.