Skip to main content
Active reading [<http://en.wikipedia.org/wiki/JavaScript>]
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132

You can use map, which is a functional programming technique that's also available in other languages like Python, and Haskell.

[1,2,3,4].map( function(item) {
     alert(item);
})

The general syntax is:

array.map(func)

In general func would take one parameter, which is an item of the array. But in the case of JavascriptJavaScript, it can take a second parameter which is the item's index, and a third parameter which is the array itself.

The return value of array.map is another array, so you can use it like this:

var x = [1,2,3,4].map( function(item) { return item * 10; } );

And now x is [10,20,30,40].

You don't have to write the function inline. It could be a separate function.

var item_processor = function(item) {
      // doDo something complicated to an item
}

new_list = my_list.map(item_processor);

which would be sort-of equivalent to:

 for (item in my_list) { item_processor(item); }

exceptExcept you don't get the new_list.

You can use map, which is a functional programming technique that's also available in other languages like Python, and Haskell.

[1,2,3,4].map( function(item) {
     alert(item);
})

The general syntax is:

array.map(func)

In general func would take one parameter, which is an item of the array. But in the case of Javascript, it can take a second parameter which is the item's index, and a third parameter which is the array itself.

The return value of array.map is another array, so you can use it like this:

var x = [1,2,3,4].map( function(item) { return item * 10; } );

And now x is [10,20,30,40].

You don't have to write the function inline. It could be a separate function.

var item_processor = function(item) {
      // do something complicated to an item
}

new_list = my_list.map(item_processor);

which would be sort-of equivalent to:

 for(item in my_list) { item_processor(item); }

except you don't get the new_list.

You can use map, which is a functional programming technique that's also available in other languages like Python and Haskell.

[1,2,3,4].map( function(item) {
     alert(item);
})

The general syntax is:

array.map(func)

In general func would take one parameter, which is an item of the array. But in the case of JavaScript, it can take a second parameter which is the item's index, and a third parameter which is the array itself.

The return value of array.map is another array, so you can use it like this:

var x = [1,2,3,4].map( function(item) {return item * 10;});

And now x is [10,20,30,40].

You don't have to write the function inline. It could be a separate function.

var item_processor = function(item) {
      // Do something complicated to an item
}

new_list = my_list.map(item_processor);

which would be sort-of equivalent to:

 for (item in my_list) {item_processor(item);}

Except you don't get the new_list.

Make the answer less bad.
Source Link
hasen
  • 164.8k
  • 66
  • 195
  • 233

You can use map (also known as apply, which is a functional programming technique that's also available in other languages like Python, and probably Haskell too).

[1,2,3,4].map( function(item) {
     alert(item);
})

The general syntax is:

array.map(func)

In general func shouldwould take one parameter, which is an item of the array. But in the case of Javascript, it can take a second parameter which is the item's index, and a third parameter which is the array itself.

The return value of array.map is another array, so you can use it like this:

var x = [1,2,3,4].map( function(item) { return item * 10; } );

And now x is [10,20,30,40].

I must clarify: This concept is from the functional paradigm.

You don't have to write the function inline; one might do so as a first sketch, but youinline. It could then extract it into its ownbe a separate function.

var item_processor = function(item) {
      // do something complicated to an item
}

new_list = my_list.map(item_processor);

which would be sort-of equivalent to:

 for(item in my_list) { item_processor(item); }

except you don't get the new_list.

You can use map (also known as apply in other languages like Python, and probably Haskell too)

[1,2,3,4].map( function(item) {
     alert(item);
})

The general syntax is:

array.map(func)

func should take one parameter.

The return value of array.map is another array, so you can use it like this:

var x = [1,2,3,4].map( function(item) { return item * 10; } );

And now x is [10,20,30,40].

I must clarify: This concept is from the functional paradigm.

You don't have to write the function inline; one might do so as a first sketch, but you could then extract it into its own function.

var item_processor = function(item) {
      // do something complicated to an item
}

new_list = my_list.map(item_processor);

which would be sort-of equivalent to:

 for(item in my_list) { item_processor(item); }

except you don't get the new_list.

You can use map, which is a functional programming technique that's also available in other languages like Python, and Haskell.

[1,2,3,4].map( function(item) {
     alert(item);
})

The general syntax is:

array.map(func)

In general func would take one parameter, which is an item of the array. But in the case of Javascript, it can take a second parameter which is the item's index, and a third parameter which is the array itself.

The return value of array.map is another array, so you can use it like this:

var x = [1,2,3,4].map( function(item) { return item * 10; } );

And now x is [10,20,30,40].

You don't have to write the function inline. It could be a separate function.

var item_processor = function(item) {
      // do something complicated to an item
}

new_list = my_list.map(item_processor);

which would be sort-of equivalent to:

 for(item in my_list) { item_processor(item); }

except you don't get the new_list.

Copy edited (e.g. ref. <http://en.wikipedia.org/wiki/Python_%28programming_language%29> and <http://en.wikipedia.org/wiki/Haskell_%28programming_language%29>). Removed historical information (e.g. ref. <http://meta.stackexchange.com/a/230693>).
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132

You can use map (also known as apply in other languages like pythonPython, and probably haskellHaskell too)

[1,2,3,4].map( function(item) {
     alert(item);
})

The general syntax is:

array.map(func)

func should take one parameter.

The return value of array.map is another array, so you can use it like this:

var x = [1,2,3,4].map( function(item) { return item * 10; } );

And now x is [10,20,30,40]

###EDIT:.

I must clarify: thisThis concept is from the functional paradigm.

You don't have to write the function inline; one might do so as a first sketch, but you could then extract it into its own function.

var item_processor = function(item) {
      // do something complicated to an item 
}

new_list = my_list.map(item_processor);

which would be sort-of equivalent to:

 for(item in my_list) { item_processor(item); }

except you don't get the new_list.

You can use map (also known as apply in other languages like python, and probably haskell too)

[1,2,3,4].map( function(item) {
     alert(item);
})

The general syntax is:

array.map(func)

func should take one parameter.

The return value of array.map is another array, so you can use it like this:

var x = [1,2,3,4].map( function(item) { return item * 10; } );

And now x is [10,20,30,40]

###EDIT:

I must clarify: this concept is from the functional paradigm.

You don't have to write the function inline; one might do so as a first sketch, but you could then extract it into its own function.

var item_processor = function(item) {
      // do something complicated to an item 
}

new_list = my_list.map(item_processor);

which would be sort-of equivalent to:

 for(item in my_list) { item_processor(item); }

except you don't get the new_list.

You can use map (also known as apply in other languages like Python, and probably Haskell too)

[1,2,3,4].map( function(item) {
     alert(item);
})

The general syntax is:

array.map(func)

func should take one parameter.

The return value of array.map is another array, so you can use it like this:

var x = [1,2,3,4].map( function(item) { return item * 10; } );

And now x is [10,20,30,40].

I must clarify: This concept is from the functional paradigm.

You don't have to write the function inline; one might do so as a first sketch, but you could then extract it into its own function.

var item_processor = function(item) {
      // do something complicated to an item
}

new_list = my_list.map(item_processor);

which would be sort-of equivalent to:

 for(item in my_list) { item_processor(item); }

except you don't get the new_list.

typo fix
Source Link
Dan Dascalescu
  • 149.6k
  • 59
  • 327
  • 415
Loading
added 503 characters in body
Source Link
hasen
  • 164.8k
  • 66
  • 195
  • 233
Loading
Source Link
hasen
  • 164.8k
  • 66
  • 195
  • 233
Loading