无法遍历JavaScript对象

时间:2016-11-01 13:55:17

标签: javascript object for-loop

作为练习,我试图学习如何打印JS对象的键和值。我很难过。

以下是我编写的基本对象,只想打印出key : value

var obTest = {
    name: "John",
    WeddingDate: "10/18/2008",
    NumberKids: "2",
    Website: "www.samplewebsite.com
};


/* VERSION 1
for (var key in obTest) {
    // skip loop if the property is from prototype
    if (!obTest.hasOwnProperty(key)) continue;

    var obKey = obTest[key];
    for (var obProp in obKey) {
        // skip loop if the obProperty is from prototype
        if(!obKey.hasOwnProperty(obProp)) continue;

        // your code
        alert(obProp + " : " + obKey[obProp]);
    }
};
    // note: this prints each character as a key:value
*/

/* VERSION 2
for (var key in obTest) {
   if (obTest.hasOwnProperty(key)) {
      var obKey = obTest[key];
      for (var prop in obKey) {
         if (obKey.hasOwnProperty(prop)) {
            console.log(prop + " : " + obKey[prop]);
         }
      }
   }
};
    // note: this prints each character as a key:value
*/


// VERSION 3
Object.keys(obTest.forEach(function(key) {
    console.log(key, obTest[key]);
}));
    // note: this gives me a breakpoint and can't figure out why it does not work

如上所述,VERSION 1和VERSION 2打印相同的输出如下:

0 : J
1 : o
2 : h
3 : n
0 : 1
1 : 0
2 : /
3 : 1
4 : 8
5 : /
6 : 2
7 : 0
8 : 0
9 : 8
0 : 2
0 : w
1 : w
2 : w
3 : .
4 : s
5 : a
6 : m
7 : p
8 : l
9 : e
10 : w
11 : e
12 : b
13 : s
14 : i
15 : t
16 : e
17 : .
18 : c
19 : o
20 : m

我使用Visual Studio Code for VERSION 3获得断点。

请帮我制作一个这样的输出:

    name : John
    WeddingDate : 10/18/2008
    NumberKids : 2
    Website : www.samplewebsite.com

我不想拥有数字键,特别是那些重复自己的数字键。我读过的其他文章似乎没有任何意义。迭代和打印对象键和值似乎非常简单。

谢谢!

5 个答案:

答案 0 :(得分:3)

你可以使用两个嵌套循环,只要一个就足够了:

for (var key in obTest) {
  // skip loop if the property is from prototype
  if (!obTest.hasOwnProperty(key)) continue;
  //find the object corresponding to the current key
  var obKey = obTest[key];
  //output the key and the corresponding object  
  alert(key + " : " + obKey);
};

使用第二个循环,您枚举所有对"键:值"在你的对象的每个值内。对于字符串" John",对键:值为(0:" J",1:" o",2:" h&#34 ;,3:" n")

对于版本3,您的括号错误:

Object.keys(obTest) //close parenthesis of keys here
      .forEach(function(key) {
         console.log(key, obTest[key]);  
       });  //close only forEach here

答案 1 :(得分:1)

第三次尝试很有希望,但实施错误。要获得所需的输出,您可以使用

function objectString(obj) {
    var keys = Object.keys(obj);
    return keys.map(v => v + ": " + obj[v]).join("\n");
}

console.log(objectString(obTest));

答案 2 :(得分:1)

使用Object.keys()获取对象的键列表。然后迭代得到它们的值。

var obTest = {
    name: "John",
    WeddingDate: "10/18/2008",
    NumberKids: "2",
    Website: "www.samplewebsite.com" };

Object.keys(obTest).forEach(function(key){        
    if (obTest.hasOwnProperty(key)){
        console.log(key + ":" + obTest[key]); 
    }
});

答案 3 :(得分:1)

只需使用MDN Docs

中的示例即可
for (var property in obTest) {
    if( obTest.hasOwnProperty( property ) ) {
       console.log(property + ": " + obTest[property])
    }
}

其中一个问题是您遗漏了“网站”属性价值的"

http://codepen.io/anon/pen/ozKENQ

答案 4 :(得分:0)

<ul class="normal-list"> <li>List Item 1</li> <li>List Item 2</li> <li class="nested-list"> <ul> <li>List Item 2.1</li> <li>List Item 2.2</li> </ul> </li> <li>List Item 3</li> <li class="nested-list"> <ul> <li>List Item 3.1</li> <li>List Item 3.2</li> </ul> </li> <li>List Item 4</li> </ul>是你的朋友,它将一个对象转换为一个数组。查看文档here

相关问题