7 Replies - 534 Views - Last Post: 27 June 2018 - 06:07 AM

#1 kayut   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 20-January 17

Map works but forEach not! Why?

Posted 18 June 2018 - 06:22 AM

Hey,

Can some one please explain to me why this works:

let myNames = ['apple', 'orange', 'lemon'];

let output = myNames.map(function(item){
	return item;
});

console.log(output);



But the same code with forEach doesn't work?

let myNames = ['apple', 'orange', 'lemon'];

let output = myNames.forEach(function(item){
	return item;
});

console.log(output);



Isn't it that myNames is an array and forEach is a built-in method of Array?
Why is that the forEach is not working?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Map works but forEach not! Why?

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4208
  • View blog
  • Posts: 13,283
  • Joined: 08-June 10

Re: Map works but forEach not! Why?

Posted 18 June 2018 - 06:35 AM

Quote

Why is that the forEach is not working?

Because it returns undefined as per specification: https://developer.mo...s/Array/forEach
resp. https://www.ecma-int...ototype.foreach

This post has been edited by Dormilich: 18 June 2018 - 06:36 AM

Was This Post Helpful? 0
  • +
  • -

#3 kayut   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 20-January 17

Re: Map works but forEach not! Why?

Posted 18 June 2018 - 08:35 AM

I know that it returns undefined.

But I'd like to know why?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4208
  • View blog
  • Posts: 13,283
  • Joined: 08-June 10

Re: Map works but forEach not! Why?

Posted 18 June 2018 - 08:48 AM

Quote

But I'd like to know why?

because someone decided so.

be aware that forEach()--unlike map()--does not do anything with the callback's return value. So it does not need to return anything.
Was This Post Helpful? 1
  • +
  • -

#5 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 850
  • View blog
  • Posts: 3,413
  • Joined: 13-June 14

Re: Map works but forEach not! Why?

Posted 18 June 2018 - 09:01 AM

forEach is used when you want to perform side effects with the values, rather than transforming them (which is what map is for).
Was This Post Helpful? 1
  • +
  • -

#6 Blindman67   User is offline

  • D.I.C Addict
  • member icon

Reputation: 140
  • View blog
  • Posts: 620
  • Joined: 15-March 14

Re: Map works but forEach not! Why?

Posted 27 June 2018 - 03:36 AM

Array.map Creates a new array that has a one to one map. Each item in the original array is mapped directly to an item in the new array.

The callback you pass to Array.map returns the new item for each original item.

For example you may have an array of values, and want to create a new array that doubles each value
const a = [1,2,3,4]; 
const b = a.map(v => v * 2); // the return value v * 2 is pushed to the new array for each item
                             // when done the new array is returned and assigned to b
// The above is the same as
const b = a.map(function(v) { return  v * 2 });


will result in a new array
b = [2, 4, 6, 8];



Array.forEach as a function does not return anything, and what you return in the callback is just ignored. It is generally used just as a for loop is.

Some people prefer
  a.forEach(v => console.log("Value : " + v))
rather than
for(const v of a) console.log("Value : " + v);


However they both do the same thing.

A complete list of the many properties and functions of Array can be found at MDN Array
Was This Post Helpful? 1
  • +
  • -

#7 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4208
  • View blog
  • Posts: 13,283
  • Joined: 08-June 10

Re: Map works but forEach not! Why?

Posted 27 June 2018 - 04:47 AM

View PostBlindman67, on 27 June 2018 - 12:36 PM, said:

However they both do the same thing.

Except that forEach() allows direct access to both the index and the value at the same time.
Was This Post Helpful? 1
  • +
  • -

#8 Blindman67   User is offline

  • D.I.C Addict
  • member icon

Reputation: 140
  • View blog
  • Posts: 620
  • Joined: 15-March 14

Re: Map works but forEach not! Why?

Posted 27 June 2018 - 06:07 AM

View PostDormilich, on 27 June 2018 - 07:47 PM, said:

Except that forEach() allows direct access to both the index and the value at the same time.


Direct access???? don't you mean, shadow access.

I'll trade you a break for some indices, and you keep the early exit throw.

However I can have my cake and eat it too..

function * each(a, i = 0) { while(i < a.length){ yield [a[i], i++] } }

const a = [1, 2, 3, 4, 5, 10];
for(const [v, i] of each(a)) console.log(v, i); 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1