How do i print the value of all properties in an object in javascript?

后端 未结 2 1750
终归单人心
终归单人心 2021-01-16 23:43

i want to use a for in oop to get the value of all the properties but do not know how. The tutorial i am following gives this example of how i can do it, but i dont underst

相关标签:
2条回答
  • 2021-01-17 00:05
    for (var property in obj){
         console.log(property + ": " + obj[property]);
    }
    

    This should do the trick, what this does is loops through the "Properties" of the object and logs the values accordingly.

    0 讨论(0)
  • 2021-01-17 00:10

    I suggest you to use variable name that has some meaning instead of x and y like:

    for(var property in object){
    console.log(object[property]);
    }
    

    for your object

    for(var prop in nyc){
    console.log(nyc[prop]);
    }
    
    0 讨论(0)
提交回复
热议问题