Miha Šušteršič Miha Šušteršič - 1 year ago 54
JSON Question

javascript: Iterating trough object returns numbers

I am trying to iterate trough this object:

"batchcomplete": "",
"continue": {
"sroffset": 10,
"continue": "-||"
"query": {
"searchinfo": {
"totalhits": 51425
"search": [{
"ns": 0,
"title": "Slovenia",
"snippet": "117; 14.817 <span class=\"searchmatch\">Slovenia</span> (i/sloʊˈviːniə, slə-, -njə/ sloh-VEE-nee-ə; Slovene: Slovenija [slɔˈʋéːnija]), officially the Republic of <span class=\"searchmatch\">Slovenia</span> (Slovene: Republika",
"size": 202115,
"wordcount": 19906,
"timestamp": "2016-09-23T19:27:27Z"
}, {
"ns": 0,
"title": "Beer in Slovenia",
"snippet": "Beer in <span class=\"searchmatch\">Slovenia</span> is dominated by the pale lager market. Most commonly known brands of <span class=\"searchmatch\">Slovenian</span> beer are Laško and Union, although smaller breweries exist",
"size": 1181,
"wordcount": 151,
"timestamp": "2016-08-18T22:05:27Z"
}, {
"ns": 0,
"title": "Statistical regions of Slovenia",
"snippet": "statistical regions of <span class=\"searchmatch\">Slovenia</span> are 12 administrative entities created in 2000 for legal and statistical purposes. By a decree of 2000 <span class=\"searchmatch\">Slovenia</span> has been divided",
"size": 3829,
"wordcount": 146,
"timestamp": "2016-03-08T10:55:17Z"
}, {
"ns": 0,
"title": "Orders, decorations, and medals of Slovenia",
"snippet": "The Republic of <span class=\"searchmatch\">Slovenia</span> has a system of orders and decorations (Slovene: Odlikovanja Republike Slovenije) for citizens who do great deeds for, or on behalf",
"size": 4977,
"wordcount": 734,
"timestamp": "2016-09-03T10:11:50Z"
}, {
"ns": 0,
"title": "Geography of Slovenia",
"snippet": "<span class=\"searchmatch\">Slovenia</span> is situated in Central Europe touching the Alps and bordering the Mediterranean. The Alps — including the Julian Alps, the Kamnik-Savinja Alps",
"size": 7776,
"wordcount": 846,
"timestamp": "2016-09-20T22:20:10Z"
}, {
"ns": 0,
"title": "Education in Slovenia",
"snippet": "Education in <span class=\"searchmatch\">Slovenia</span> from primary to secondary schooling is regulated by the National Education Institute of the Republic of <span class=\"searchmatch\">Slovenia</span> (ZRSŠ), whose scope",
"size": 5017,
"wordcount": 708,
"timestamp": "2016-07-30T16:21:57Z"
}, {
"ns": 0,
"title": "Subdivisions of Slovenia",
"snippet": "Subdivisions of <span class=\"searchmatch\">Slovenia</span>: Cadastral community Municipalities of <span class=\"searchmatch\">Slovenia</span> NUTS of <span class=\"searchmatch\">Slovenia</span> Statistical regions of <span class=\"searchmatch\">Slovenia</span> ISO 3166-2:SI Six telephone",
"size": 416,
"wordcount": 27,
"timestamp": "2016-05-17T17:11:00Z"
}, {
"ns": 0,
"title": "Mexico–Slovenia relations",
"snippet": "Mexico–<span class=\"searchmatch\">Slovenia</span> relations refers to foreign relations between Mexico and <span class=\"searchmatch\">Slovenia</span>. Both nations are members of the Organisation for Economic Co-operation",
"size": 4604,
"wordcount": 494,
"timestamp": "2015-10-25T14:14:48Z"
}, {
"ns": 0,
"title": "Football Association of Slovenia",
"snippet": "The Football Association of <span class=\"searchmatch\">Slovenia</span> (Slovene: Nogometna zveza Slovenije or NZS) is the governing body of football in <span class=\"searchmatch\">Slovenia</span>. It organizes the following",
"size": 2059,
"wordcount": 150,
"timestamp": "2016-09-14T20:14:33Z"
}, {
"ns": 0,
"title": "Archaeological Society of Slovenia",
"snippet": "The Archaeological Society of <span class=\"searchmatch\">Slovenia</span> is a non-governmental organization that unites Slovene archaeologists on a voluntary basis. The society organises",
"size": 3465,
"wordcount": 372,
"timestamp": "2016-05-16T20:58:03Z"

What I would like to do is to get the title of all the pages returned under

This is my function:

function makeLinks(data) {
for(var page in data.query.search) {

For some reason, instead of returning all the data (ns, title, snippet...) this just returns:
, etc. Also,
. What am I doing wrong?

Answer Source

You are printing the indexes instead of the actual objects. My favorite way is to use forEach loop like:

     function makeLinks(data) {
        data.query.search.forEach(function(item) { 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download