我怎样才能遍历对象?

时间:2017-07-05 04:26:16

标签: javascript object

我关注对象

Object {  
   14533:Object,
   14598:Object,
   43281:Object,
   275047:Object,
   553883:Object,
   602381:Object,
   602639:Object,
   848489:Object,
   16891417:Object,
   26457880:Object
}

,其中

14533 {  
   pageid:14533,
   ns:0,
   title:"India",
   index:1,
   extract:"India, officially the Republic of I…"
}

我想迭代这10个对象中的每一个并提取pageid,title,extract

1 个答案:

答案 0 :(得分:-3)

此问题是Iterate through object properties

的副本

在提问之前做一些搜索,否则你将被投票。

<强>更新

正如您将在链接中找到的解决问题的关键因素是:

for(var key in obj){
    if (obj.hasOwnProperty(key)) {
        // your code
    }
}

完整的解决方案看起来像这样:

for(var key in obj){
    if (obj.hasOwnProperty(key)) {
        var thisObj = obj[key];
        var thisPageId = thisObj.pageid;
        var thisTitle = thisObj.title;
        var thisExtract = thisObj.extract;

        // console test
        console.log(key, thisPageId, thisTitle, thisExtract);
    }
}