How do I loop through or enumerate a JavaScript object?

You can use the for-in loop as shown by others. However, you also have to make sure that the key you get is an actual property of an object, and doesn’t come from the prototype. Here is the snippet: For-of with Object.keys() alternative: Notice the use of for-of instead of for-in, if not used it will return undefined on named … Read more

C++ for each, pulling from vector elements

For next examples assumed that you use C++11. Example with ranged-based for loops: You should use const auto &attack depending on the behavior of makeDamage(). You can use std::for_each from standard library + lambdas: If you are uncomfortable using std::for_each, you can loop over m_attack using iterators: Use m_attack.cbegin() and m_attack.cend() to get const iterators.