对于嵌套对象javascript don';t返回期望值
我试图遍历一个JS对象,循环中有for_。 它似乎以纯文本形式返回找到的值(contentCard1)。 我无法让它打印val.text对于嵌套对象javascript don';t返回期望值,javascript,object,nested,Javascript,Object,Nested,我试图遍历一个JS对象,循环中有for_。 它似乎以纯文本形式返回找到的值(contentCard1)。 我无法让它打印val.text var contentCards = { contentCard1: {text: "text in here", date: "date in here"} } for(var val in contentCards) { console.log(val.text); } 记录val.text会给我未定义的,记录val会给我content
var contentCards = {
contentCard1: {text: "text in here", date: "date in here"}
}
for(var val in contentCards) {
console.log(val.text);
}
记录val.text
会给我未定义的,记录val
会给我contentCard1
感谢您的帮助。使用,您正在迭代内容卡的键。对于访问,您需要使用对象和密钥
var contentCards={contentCard1:{text:“text in here”,date:“date in here”};
for(内容卡中的var val){
console.log(contentCards[val].text);
}
.as控制台包装{max height:100%!important;top:0;}
使用了以下内容:
var contentCards = {
contentCard1: { text: "text in here", date: "date in here" }
}
alert(contentCards.contentCard1.text);
这并不是那个问题的翻版,尽管很明显有重叠。@nnnnnn我的道歉。:-)当dupe回答如何在嵌套对象上循环时仍将保留投票权。嗯?如果不使用迭代器变量而直接访问特定的嵌套属性,循环的意义何在?是的。但是我认为您有contentCards作为列表,如果您在contentCards
中有val,然后从不使用val
,那么循环的意义是什么?每次迭代都会提醒相同的特定值。
var contentCards = {
contentCard1: { text: "text in here", date: "date in here" }
}
alert(contentCards.contentCard1.text);