Skip to main content
Clarify how map works, as it doesn't technically modify anything
Source Link
CherryDT
  • 29.4k
  • 5
  • 56
  • 86

A way closest to your idea would be to use Array.forEach() which accepts a closure function which will be executed for each element of the array.

myArray.forEach( (item) => { // Do something console.log(item); } ); 

Another viable way would be to use Array.map() which works in the same way, but it also modifies each elementtakes all values that you return and returns it (resultingthem in a new array (essentially mapping each element to a new one), like this:

var myArray = [1, 2, 3]; myArray = myArray.map( (item) => { return item + 1; } ); console.log(myArray); // [2, 3, 4] 

A way closest to your idea would be to use Array.forEach() which accepts a closure function which will be executed for each element of the array.

myArray.forEach( (item) => { // Do something console.log(item); } ); 

Another viable way would be to use Array.map() which works in the same way, but it also modifies each element and returns it (resulting in a new array) like:

var myArray = [1, 2, 3]; myArray = myArray.map( (item) => { return item + 1; } ); console.log(myArray); // [2, 3, 4] 

A way closest to your idea would be to use Array.forEach() which accepts a closure function which will be executed for each element of the array.

myArray.forEach( (item) => { // Do something console.log(item); } ); 

Another viable way would be to use Array.map() which works in the same way, but it also takes all values that you return and returns them in a new array (essentially mapping each element to a new one), like this:

var myArray = [1, 2, 3]; myArray = myArray.map( (item) => { return item + 1; } ); console.log(myArray); // [2, 3, 4] 
added 28 characters in body
Source Link
anteAdamovic
  • 1.4k
  • 12
  • 25

A way closest to your idea would be to use Array.forEach() which accepts a closure function which will be executed for each element of the array.

myArray.forEach( (item) => { // Do something console.log(item); } ); 

Another viable way would be to use Array.map() which works in the same way, but it also mutatesmodifies each element and returns it (resulting in a new array) like:

var myArray = [1, 2, 3]; myArray = myArray.map( (item) => { return item + 1; } ); console.log(myArray); // [2, 3, 4] 

A way closest to your idea would be to use Array.forEach() which accepts a closure function which will be executed for each element of the array.

myArray.forEach( (item) => { // Do something console.log(item); } ); 

Another viable way would be to use Array.map() which works in the same way, but it also mutates each element and returns it like:

var myArray = [1, 2, 3]; myArray = myArray.map( (item) => { return item + 1; } ); console.log(myArray); // [2, 3, 4] 

A way closest to your idea would be to use Array.forEach() which accepts a closure function which will be executed for each element of the array.

myArray.forEach( (item) => { // Do something console.log(item); } ); 

Another viable way would be to use Array.map() which works in the same way, but it also modifies each element and returns it (resulting in a new array) like:

var myArray = [1, 2, 3]; myArray = myArray.map( (item) => { return item + 1; } ); console.log(myArray); // [2, 3, 4] 
Active reading [<https://en.wikipedia.org/wiki/Closure_(computer_programming)> <http://en.wiktionary.org/wiki/which>].
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

A way closest to your idea would be to use Array.forEach() which accepts a clojureclosure function wichwhich will be executed for each element of the array.

myArray.forEach( (item) => { // doDo something  console.log(item); } ); 

Another viable way would be to use Array.map() which works in the same way, but it also mutates each element and returns it like:

var myArray = [1, 2, 3]; myArray = myArray.map( (item) => { return item + 1; } ); console.log(myArray); // [2, 3, 4] 

A way closest to your idea would be to use Array.forEach() which accepts a clojure function wich will be executed for each element of the array.

myArray.forEach( (item) => { // do something  console.log(item); } ); 

Another viable way would be to use Array.map() which works in the same way but also mutates each element and returns it like:

var myArray = [1, 2, 3]; myArray = myArray.map( (item) => { return item + 1; } ); console.log(myArray); // [2, 3, 4] 

A way closest to your idea would be to use Array.forEach() which accepts a closure function which will be executed for each element of the array.

myArray.forEach( (item) => { // Do something console.log(item); } ); 

Another viable way would be to use Array.map() which works in the same way, but it also mutates each element and returns it like:

var myArray = [1, 2, 3]; myArray = myArray.map( (item) => { return item + 1; } ); console.log(myArray); // [2, 3, 4] 
Source Link
anteAdamovic
  • 1.4k
  • 12
  • 25
Loading