在Javascript对象中打印所有属性 [英] Printing all properties in a Javascript Object

查看:745
本文介绍了在Javascript对象中打印所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注代码学院教程,我发现这很困难。

I am following a code academy tutorial and i am finding this difficult.

作业如下:


使用for-in循环打印出nyc的所有属性。

Use a for-in loop to print out all the properties of nyc.



var nyc = {
    fullName: "New York City",
    mayor: "Michael Bloomberg",
    population: 8000000,
    boroughs: 5
};

// write your for-in loop here
for (var  in nyc){
    console.log(population);
}


推荐答案

您的语法不正确。 for 循环中的 var 关键字必须后跟变量名称,在本例中为 propName

Your syntax is incorrect. The var keyword in your for loop must be followed by a variable name, in this case its propName

var propValue;
for(var propName in nyc) {
    propValue = nyc[propName]

    console.log(propName,propValue);
}

我建议你看一下基础知识:

I suggest you have a look here for some basics:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

这篇关于在Javascript对象中打印所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆