【问题标题】:JS display of objects and the property of eachJS显示对象和每个属性
【发布时间】:2016-06-12 23:12:18
【问题描述】:

所以新手试图理解 Javascript 的早期步骤。我正在尝试了解对象数组以及如何显示对象及其属性。例子是显示水果的颜色:

var fruitColor = {'apples':'red', 'bananas':'yellow', 'grapes':'purple'};


//answer would be string and then the properties...
//"Color of fruit: apples - red, bananas - yellow, grapes - purple"

【问题讨论】:

标签: javascript arrays object properties


【解决方案1】:

类似:

var fruitColor = {'apples':'red', 
                  'bananas':'yellow', 
                  'grapes':'purple'
                  };
var sentence="Color of fruit: ";
for(var fruit in fruitColor){
    sentence+= fruit + " - " + fruitColor[fruit] + ", ";
}
sentence=sentence.substring(0, sentence.length - 2); 
// remove the last ','

查看for...in 循环

https://jsfiddle.net/n0z13ge6/

【讨论】:

    猜你喜欢
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 2015-05-17
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 2018-09-24
    相关资源
    最近更新 更多