foreach and replace item based on condition 1

foreach and replace item based on condition

arr.forEach(function(part, index, theArray) {
  theArray[index] = "hello world";
});

Here is what the above code is Doing:
1. We create an array called arr.
2. We call the forEach() method on the arr.
3. The forEach() method takes a callback function as an argument.
4. The callback function takes up to three arguments:
– The first argument represents each element in the array (per loop iteration).
– The second argument represents the index of said element.
– The third argument represents the array that .forEach() was called upon.
5. The callback function replaces each element in the array with the string “hello world”.
6. The forEach() method then loops through the array, replacing each element as it goes.

Similar Posts