Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what use does the javascript forEach method have (that map can't do)?

The only difference I see in map and foreach is that map is returning an array and forEach is not. However, I don't even understand the last line of the forEach method "func.call(scope, this[i], i, this);". For example, isn't "this" and "scope" referring to same object and isn't this[i] and i referring to the current value in the loop?

I noticed on another post someone said "Use forEach when you want to do something on the basis of each element of the list. You might be adding things to the page, for example. Essentially, it's great for when you want "side effects". I don't know what is meant by side effects.

Array.prototype.map = function(fnc) {     var a = new Array(this.length);     for (var i = 0; i < this.length; i++) {         a[i] = fnc(this[i]);     }     return a; }  Array.prototype.forEach = function(func, scope) {     scope = scope || this;     for (var i = 0, l = this.length; i < l; i++) {         func.call(scope, this[i], i, this);     } } 

Finally, are there any real uses for these methods in JavaScript (since we aren't updating a database) other than to manipulate numbers like the following?

alert([1,2,3,4].map(function(x){ return x + 1})); // This is the only example I ever see of map in JavaScript. 
like image 329
JohnMerlino Avatar asked Jun 13 '10 23:06

JohnMerlino


People also ask

What is difference between forEach and map in JavaScript?

Differences between forEach() and map() methods:The forEach() method does not create a new array based on the given array. The map() method creates an entirely new array. The forEach() method returns “undefined“. The map() method returns the newly created array according to the provided callback function.

What is the use of forEach in JavaScript?

JavaScript Array forEach() The forEach() method calls a function for each element in an array. The forEach() method is not executed for empty elements.

What is the difference between forEach and map method?

The main difference between map and forEach is that the map method returns a new array by applying the callback function on each element of an array, while the forEach method doesn't return anything. You can use the forEach method to mutate the source array, but this isn't really the way it's meant to be used.

What can forEach be used for?

Foreach loop (or for each loop) is a control flow statement for traversing items in a collection. Foreach is usually used in place of a standard for loop statement.


1 Answers

The essential difference between map and forEach in your example is that forEach operates on the original array elements, whereas map explicitly returns a new array as a result.

With forEach you are taking some action with -- and optionally changing -- each element in the original array. The forEach method runs the function you provide for each element, but returns nothing (undefined). On the other hand, map walks through the array, applies a function to each element, and emits the result as a new array.

The "side effect" with forEach is that the original array is being changed. "No side effect" with map means that, in idiomatic usage, the original array elements are not changed; the new array is a one-to-one mapping of each element in the original array -- the mapping transform being your provided function.

The fact that there's no database involved does not mean that you won't have to operate on data structures, which, after all, is one of the essences of programming in any language. As for your last question, your array can contain not only numbers, but objects, strings, functions, etc.

like image 187
Ken Redler Avatar answered Sep 29 '22 09:09

Ken Redler