According to the MDN page at for each...in loop, this construct is deprecated. Is there an alternative that does exactly the same thing? The for...of loop does not iterate over non-integer (own enumerable) properties. If there isn't an alternative, why did they deprecate it then?
Use map() instead of for() loops Before I get started, this is going to teach you how the . map() function works. If you only have knowledge of for() loops in JavaScript, this article will require you to understand the Arrow Function Expression syntax (a.k.a. “fat arrow” functions).
The most common way was to use for loop, iterate elements and find which is even. Instead of using for in this case, we can use the filter() method. This method also returns a new array.
map() is an alternative to for loop when it comes to code organisation but doesn't do well when memory, speed is taken into account.
The JavaScript forEach method is one of the several ways to loop through arrays. Each method has different features, and it is up to you, depending on what you're doing, to decide which one to use.
To iterate over all the properties of an object obj
, you may do this :
for (var key in obj) {
console.log(key, obj[key]);
}
If you want to avoid inherited properties, you may do this :
for (var key in obj) {
if (!obj.hasOwnProperty(key)) continue;
console.log(key, obj[key]);
}
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