javascript:迭代对象返回数字

标签 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"
    }]
  }
}

我想做的是获取搜索下返回的所有页面的标题。

这是我的功能:

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

出于某种原因,它没有返回所有数据(ns、标题、片段...),而是返回:'0''1' 等.此外,page.title 返回undefined。我做错了什么?

最佳答案

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

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

关于javascript:迭代对象返回数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39704179/

相关文章:

JavaScript jQuery 变量我应该避免使用与全局变量冲突

javascript - Jest 的 `it.each()` 描述以在称为 $predicate 时呈现箭头函数源代码

php - 如何在 PHP 中迭代数组并获取键值

json - 将复杂 JSON 中的每个值提取到 Swift 中的数组中

json - 用 JSON 表示图形

javascript - 函数变量签名/参数

javascript - 动画未通过 jQuery TransitionEnd 事件完成

javascript - 这段代码有什么问题? (Jquery)

javascript - 如何将 HTML 表单(JSON 格式)中的数据分配给变量?

c# - [JsonProperty] 在 C# 中有何用途?