What's the usual thing you do with an array? Iterate through its items! This is where 4 array method can be helpful. This post describes how to use 4 array method to iterate items of an array in JavaScript. Plus, you will read about 4 best practices like correct handling of 7 and how to iterate array-like objects. 1. Basic forEach example8 method iterates over the array items, in ascending order, without mutating the array. The first argument of 4 is the callback function called for every item in the array. The second argument (optional) is the value of 7 set in the callback.
Let's see how 4 works in practice. In the following example, 2 array has 3 items. Let's use 4 to log to console every color:
4 a the callback function. 5 executes 4 function for every item in 2, setting the iterated item as the first argument. This way, 3 invocation of 8 function are perfomed:
That's how, in a few words, 4 method works. 2. Index of the iterated element3 executes the
Let's access the index of each item in the colors array:
8 function has access to the current iterated item and the index. The callback is executed 3 times:
3. Access the array inside the callbackTo access the array itself during the iteration, you can use the 3rd parameter inside the callback function. Let's log the message 9 when JavaScript executes the last iteration on the array items.
The 3rd parameter 0 inside the callback function is the array on which 4 method was called on. 4. this inside the callbackLet's run the following example in a browser, and pay attention to the value of 7:
7 inside 8 equals to 5, which is the global object in the browser environment. Follow to get more information. In some situations, you might need to set 7 to an object of interest. Then indicate this object as the second argument when calling 4:
Let's implement a 8 class, which always holds an unique list of items:
9 is called with the second argument pointing to 7, i.e. the instance of 8 class. Inside the callback of 4, 7 points also to an instance of 8. Now it's safe to access 5. Note that for the above example using an arrow function as the callback of 4 would be better. The the value of 7 from the lexical scope, so there's no need to use the second argument on 4. 5. forEach skips empty slots4 skips the empty slots of the array (named ).
0 contains 1, an empty slot, and 2. 4 iterates over 1 and 2, but skips the empty slot. 6. Iterate array-like objects using forEach4 can iterate over array-like objects:
7 is an array-like object. In order to iterate over its items, you have to call indirectly 4 using the 9. The 4 method is taken from 1. Alternatively, you can transform the array-like object into an array using 2, then iterate: 0 7. When to use forEach()4 is best used to iterate array items, without breaking, and having simultaneously some side-effect. Side-effects examples are a mutation of an outer scope variable, I/O operations (HTTP requests), DOM manipulations, and alike. For example, let's select all input elements from the DOM and use 4 to clear them: 1 The side effect in the callback function is clearing the value of the input field. Keep in mind that you cannot normally break the iteration of 4 (other than a tricky way to throw an error to stop the iteration, which is a cheap hack). The method will always iterate over all the items. If your case requires an early break from the cycle, a better option is the classic for or for..of. When the array iteration computes a result, without side-effects, a better alternative is to select an array method like:
For example, let's determine whether all numbers of an array are even. The first solution involves 4 method: 2 The code determines correctly if all numbers are even. The problem is the impossibility to break after finding the first odd number 2. For this situation, a better alternative is 8 method: 3 8 doesn't only make the code shorter. It is also optimal, because 0 method breaks iterating after finding the first odd number. 8. Conclusion3 method is an efficient way to iterate over all array items. Its first argument is the 4 function, which is invoked for every item in the array with 3 arguments: item, index, and the array itself. 4 is useful to iterate over all array items, without breaking, involving simultaneously some side-effects. Otherwise, consider an alternative array method. Do you know good uses cases of 4? Write them in a comment below. Like the post? Please share!Suggest Improvement Quality posts into your inboxI regularly publish posts containing:
Subscribe to my newsletter to get them right into your inbox. SubscribeJoin 6861 other subscribers. About Dmitri PavlutinTech writer and coach. My daily routine consists of (but not limited to) drinking coffee, coding, writing, coaching, overcoming boredom 😉. Now working as a full-time open source developer on Readapt project - aiming to make the web accessible for everyone. |