我正在尝试使用for_in循环遍历JS对象。似乎正在以纯文本形式返回找到的值(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
给了我undefined
,日志val
给了我contentCard1
。
感谢您的帮助。
使用for ... in
,您正在遍历的键contentCards
。对于访问,您需要带括号符号的对象和键。
contentCards[val].text
// ^^^^^
var contentCards = { contentCard1: { text: "text in here", date: "date in here" } };
for (var val in contentCards) {
console.log(contentCards[val].text);
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句