首先,嘿!我在这里已经问过两次了,都得到了很好的解答。
所以...我想在控制台上进行for循环打印,将所有变量记录在commands
变量上。
我想只打印BOH
,HALO
和BOOM HSAKALAKA
变量,而不是他们的文本:BOH!
,HALO!
,BOOM SHAKALAKA!
。
var commands = {
'BOH': {text: 'BOH!'},
'HALO': {text: 'HALO!'},
'BOOM SHAKALAKA': {text: 'BOOM SHAKALAKA!'},
};
for (number = 0; number < commands.lenght; number++){
console.log(commands[number]);
};
像这样的演示?
var commands = {
'BOH)': {text: 'BOH!'},
'HALO': {text: 'HALO!'},
'BOOM SHAKALAKA': {text: 'BOOM SHAKALAKA!'},
};
for(key in commands){
if(commands.hasOwnProperty(key)){ //get only the properties of the current object and skip inherited properties
console.log("variable - " + key + " " + "value - " + commands[key].text);
}
};
在您的示例中,您正在遍历一个不存在的数组。commands
不是数组,而是对象。因此,为了遍历一个对象,我们应该使用它的key
属性。
在我们的例子中,key
is'BOH)'
的值key
是commands[key]
=> BOH!
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句