如何获取此对象中的值?

时间:2016-01-21 19:04:32

标签: javascript

我一直试图从下面的对象中获取值而没有任何运气。我知道有很多答案,但我是一个视觉人,他们不适合我的布局。

另外,如何解决对象以更改“ringAlarm”的值?

var strRingAlarm = {
    sq1 :{ringAlarm:"OFF", alarmName:"Chime1"},
    sq2 :{ringAlarm:"NO", alarmName:"Chime2"},
    sq3 :{ringAlarm:"NO", alarmName:"Chime3"},
    sq4 :{ringAlarm:"NO", alarmName:"Chime4"},
    sq5 :{ringAlarm:"NO", alarmName:"Chime5"},
    sq6 :{ringAlarm:"NO", alarmName:"Chime6"},
    sq7 :{ringAlarm:"NO", alarmName:"Chime7"},
    sq8 :{ringAlarm:"NO", alarmName:"Chime8"},
    sq9 :{ringAlarm:"NO", alarmName:"Chime9"},
    sq10:{ringAlarm:"NO", alarmName:"Chime10"},
    sq11:{ringAlarm:"NO", alarmName:"Chime11"},
    sq12:{ringAlarm:"NO", alarmName:"Chime11"}
};
        Object.getOwnPropertyNames(strRingAlarm).forEach(function(val, idx, array) {
        console.log(val + ' -> ' + val.ringAlarm);
 }
)

4 个答案:

答案 0 :(得分:1)

var strRingAlarm = {
    sq1: {ringAlarm:"OFF", alarmName:"Chime1"},
    sq2: {ringAlarm:"NO", alarmName:"Chime2"},
    sq3: {ringAlarm:"NO", alarmName:"Chime3"},
    sq4: {ringAlarm:"NO", alarmName:"Chime4"},
    sq5: {ringAlarm:"NO", alarmName:"Chime5"},
    sq6: {ringAlarm:"NO", alarmName:"Chime6"},
    sq7: {ringAlarm:"NO", alarmName:"Chime7"},
    sq8: {ringAlarm:"NO", alarmName:"Chime8"},
    sq9: {ringAlarm:"NO", alarmName:"Chime9"},
    sq10: {ringAlarm:"NO", alarmName:"Chime10"},
    sq11: {ringAlarm:"NO", alarmName:"Chime11"},
    sq12: {ringAlarm:"NO", alarmName:"Chime11"}
};


Object.getOwnPropertyNames(strRingAlarm).forEach(function(val, idx, array) {
    console.log(val + ' -> ' + strRingAlarm[val].ringAlarm) 
})

答案 1 :(得分:0)

你可以像这样获得ringAlarmProperty

 for(var prop in strRingAlarm){
   console.log(strRingAlarm[prop].ringAlarm)
 }

答案 2 :(得分:0)

var strRingAlarm = {
    sq1 :{ringAlarm:"OFF", alarmName:"Chime1"},
    sq2 :{ringAlarm:"NO", alarmName:"Chime2"},
    sq3 :{ringAlarm:"NO", alarmName:"Chime3"},
    sq4 :{ringAlarm:"NO", alarmName:"Chime4"},
    sq5 :{ringAlarm:"NO", alarmName:"Chime5"},
    sq6 :{ringAlarm:"NO", alarmName:"Chime6"},
    sq7 :{ringAlarm:"NO", alarmName:"Chime7"},
    sq8 :{ringAlarm:"NO", alarmName:"Chime8"},
    sq9 :{ringAlarm:"NO", alarmName:"Chime9"},
    sq10:{ringAlarm:"NO", alarmName:"Chime10"},
    sq11:{ringAlarm:"NO", alarmName:"Chime11"},
    sq12:{ringAlarm:"NO", alarmName:"Chime11"}
};


for(key in strRingAlarm){
  console.log(key + "..." +strRingAlarm[key]['ringAlarm'])
}

答案 3 :(得分:0)

如果您拥有对象name的属性的obj,则获取该属性值的最佳方法是obj[name]。这样,试试这个代码块:

Object.getOwnPropertyNames(strRingAlarm).forEach(function(val, idx, array) {
  console.log(val + ' -> ' + strRingAlarm[val].ringAlarm);
});