javascript:迭代通过对象返回数字

时间:2016-09-26 13:26:38

标签: javascript json

我正在尝试遍历此对象:

{
  "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"
    }]
  }
}

我想要的是获取search下返回的所有网页的标题。

这是我的功能:

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

出于某种原因,不是返回所有数据(ns,title,snippet ......),而是返回:'0''1'等。此外,page.title返回{ {1}}。我做错了什么?

4 个答案:

答案 0 :(得分:1)

您正在打印索引而不是实际对象。我最喜欢的方法是使用forEach循环:

     function makeLinks(data) {
        data.query.search.forEach(function(item) { 
           console.log(item)
        });
     }

答案 1 :(得分:0)

你实际上在数组中循环运行,这就是为什么你得到索引而不是对象。

尝试这样做:

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

修改

@Jaromanda X提到了一个很好的观点,为什么不在数组中使用for,所以最好使用这个代码:

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

答案 2 :(得分:0)

for-in循环将键放在循环变量中。 data.query.search是一个数组。它们对该数组的键是数字。这就是你所看到的。

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

    console.log(object);
  }
}

forEach更接近您最初的尝试:

function printSomeStuff(data) {
  data.query.search.forEach(function(object) {
    console.log(object);
  })
}

答案 3 :(得分:0)

在您的对象中,data.search表示一个对象数组。您可以迭代该数组并记录每个页面:

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