rob.m rob.m - 3 years ago 111
Javascript Question

How to get objects data after an api call?

I am running the following:

var myTitle = "Battaglia di Forte Ṭabarsí";
var langList;
var url="https://it.wikipedia.org/w/api.php?action=parse&disablelimitreport=true&format=json&prop=text|langlinks&noimages=true&mobileformat=true&page="+ myTitle + "&callback=?";
$.getJSON(url,function(data){
jQuery.each(data, function(i, item) {
langList = item.langlinks;
});
console.log(langList);
});


I can see the page has different languages and other properties that I can get when I use the parameter
langlinks
in the api but then in the console I get:


(6) [Object, Object, Object, Object, Object, Object]


I would like to access that data, like title in another language and basically to be able to output any objects and more specifically any properties of each object.

Here it is a jsFiddle, check the console.

Answer Source

data is an object. It contains prase(property), which itself is an object, containing langLinks(property) : Array of objects

If you want to access langLinks items, use the index notation [ ] like so: langLinks[index] There is no need for jQuery.each(d... you can access specific language by filtering langLinks

data.parse.langlinks.filter(function(val){ return val.lang === 'en'});

When accessing object's proparties, use the dot or index notation (val.lang)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download