Sometimes its useful to loop over all the keys of an object. This is most useful when you're using an object as you would use a dictionary or hash map in other languages.
let titan = {
name: "Eren",
power: "Attack Titan",
age: 19,
};
for (const key in titan) {
console.log(`${key}: ${titan[key]}`);
}
// name: Eren
// power: Attack Titan
// age: 19
In modern specifications, the traversal order is well-defined and consistent across implementations:
Within each component of the prototype chain, all non-negative integer keys (those that can be array indices) will be traversed first in ascending order by value, then other string keys in ascending chronological order of property creation.
That's still not obvious to me when I read code however, so if I really care about order I usually break out the keys into an array and sort them how I want. We'll cover arrays later.
Tom has been storing Textio's server logs as structured objects. Of course, the properties have changed over time, causing each log entry to have a different set. Thanks, Tom.
Complete the printMatchingProperties function. If a key in the object starts with the given searchTerm, it should print the key/value pair in this format:
Found: <key> -> <value>
Where <key> is the property name and <value> is its corresponding value. For example, this:
printMatchingProperties(
{
messageId: "abc123",
messageText: "Your order has shipped",
timestamp: "2025-02-06T12:34:56Z",
sender: "TextioBot",
},
"message",
);
should print:
Found: messageId -> abc123
Found: messageText -> Your order has shipped
You can use String.prototype.startsWith() to check if a string starts with a given substring.