I am trying to walk through a JS object, with the for_in loop. That seems to be returning the found value (contentCard1) as plain text. I can't get it to print val.text
var contentCards = {
contentCard1: {text: "text in here", date: "date in here"}
}
for(var val in contentCards) {
console.log(val.text);
}
Logging val.text gives me undefined, and logging just val gives me contentCard1.
Thanks for helping.
With for ... in, you are iterating over the keys of contentCards. For the access you need the object and the key with bracket notation.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With