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

foreach vs someList.ForEach(){}

There are apparently many ways to iterate over a collection. Curious if there are any differences, or why you’d use one way over the other. First type: Other Way: I suppose off the top of my head, that instead of the anonymous delegate I use above, you’d have a reusable delegate you could specify…

How to iterate over a JavaScript object?

For most objects, use for .. in : With ES6, if you need both keys and values simultaneously, do To avoid logging inherited properties, check with hasOwnProperty : You don’t need to check hasOwnProperty when iterating on keys if you’re using a simple object (for example one you made yourself with {}). This MDN documentation explains more generally how to deal with objects … Read more

Good input validation loop using cin – C++

I’m not a huge fan of turning on exceptions for iostreams. I/O errors aren’t exceptional enough, in that errors are often very likely. I prefer only to use exceptions for less frequent error conditions. The code isn’t bad, but skipping 80 characters is a bit arbitrary, and the error variable isn’t necessary if you fiddle … Read more