0

I am trying to iterate through 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 search.

This is my function:

function makeLinks(data) {
  for(var page in data.query.search) {
    console.log(page);
  }
}

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Miha Šušteršič
  • 9,742
  • 25
  • 92
  • 163
  • 2
    `data.query.search` is an array of objects. Either do `console.log(data.query.search[page])` (not the best way to iterate an array) or use array iterators like map, forEach, etc. – Sterling Archer Sep 26 '16 at 13:30
  • the properties of an array are the indices of the items ... 0, 1, 2 ... array.length - 1 - you could use `for...of` or `data.query.search.forEach(function(item) { ... });` – Jaromanda X Sep 26 '16 at 13:30
  • Your sample data is invalid, `snippet: "117; 14.817 Slovenia` is wrong for example. Voting to close because it lacks an [MCVE](http://stackoverflow.com/help/mcve). – user247702 Sep 26 '16 at 13:33

4 Answers4

1

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) { 
           console.log(item)
        });
     }
callback
  • 3,981
  • 1
  • 31
  • 55
0

You actually run for in loop on an array that is why you get indexes instead of objects.

Try doing this:

function makeLinks(data) {
  var searchData = data.query.search;
  for(var i in searchData) {
    var page = searchData[i];
    console.log(page);
    }
  };

EDIT:

@Jaromanda X mentioned a good point for why not using for in over an array so it's better to use this code:

function makeLinks(data) {
  var searchData = data.query.search;
  for(var i = 0; i < searchData.length; i++ ) {
    var page = searchData[i];
    console.log(page);
    }
  };
Shlomi Haver
  • 954
  • 9
  • 14
  • see [Why is using “for…in” with array iteration a bad idea?](http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea) – Jaromanda X Sep 26 '16 at 13:32
0

for-in loops place the key in the loop variable. data.query.search is an array. They keys to that array are numbers. that is what you ware seeing.

function printSomeStuff(data) {
  for(var key in data.query.search) {
    var object = data.query.search[key];

    console.log(object);
  }
}

forEach is closer to what you were originally trying:

function printSomeStuff(data) {
  data.query.search.forEach(function(object) {
    console.log(object);
  })
}
Potter
  • 633
  • 7
  • 13
0

In your object, data.search represents an array of objects. You can iterate over that array and log every page:

function makeLinks(data){
  data.query.search.forEach(page => {
    console.log(page);
  });
}