Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index inside map() function

I am missing a option how to get the index number inside the map function using List from Immutable.js:

var list2 = list1.map(mapper => { a: mapper.a, b: mapper.index??? }).toList();

Documentation shows that map() returns Iterable<number, M>. Is there any elegant way to what I need?

like image 804
Zygimantas Avatar asked Jul 14 '16 02:07

Zygimantas


People also ask

Can we get index in map?

Getting the index in map()We can get the index of the current iteration using the 2nd parameter of the map() function.

What is an index on a map?

index map. [cartography] A schematic map used as a reference for a collection of map sheets, outlining the total area covered along with the coverage extent of, and usually a name or reference for, each map sheet.

How do you pass an index on a map in react?

To use the map() method with index in React: Call the map() method on the array. The function you pass to the map() method gets called with the element and index.

How do you find the index of an element on a map?

int k = distance(mymap. begin(), mymap. find(mykey)); It will give you the index of the key element.


2 Answers

You will be able to get the current iteration's index for the map method through its 2nd parameter.

Example:

const list = [ 'h', 'e', 'l', 'l', 'o']; list.map((currElement, index) => {   console.log("The current iteration is: " + index);   console.log("The current element is: " + currElement);   console.log("\n");   return currElement; //equivalent to list[index] }); 

Output:

The current iteration is: 0 <br>The current element is: h  The current iteration is: 1 <br>The current element is: e  The current iteration is: 2 <br>The current element is: l  The current iteration is: 3 <br>The current element is: l   The current iteration is: 4 <br>The current element is: o 

See also: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Parameters

callback - Function that produces an element of the new Array, taking three arguments:

1) currentValue
The current element being processed in the array.

2) index
The index of the current element being processed in the array.

3) array
The array map was called upon.

like image 167
Samuel Toh Avatar answered Sep 21 '22 08:09

Samuel Toh


Array.prototype.map() index:

One can access the index Array.prototype.map() via the second argument of the callback function. Here is an example:

const array = [1, 2, 3, 4];  const map = array.map((x, index) => {   console.log(index);   return x + index; });  console.log(map);

Other arguments of Array.prototype.map():

  • The third argument of the callback function exposes the array on which map was called upon
  • The second argument of Array.map() is a object which will be the this value for the callback function. Keep in mind that you have to use the regular function keyword in order to declare the callback since an arrow function doesn't have its own binding to the this keyword.

For example:

const array = [1, 2, 3, 4];  const thisObj = { prop1: 1 }  const map = array.map((x, index, array) => {   console.log(array);   console.log(this) }, thisObj);
like image 28
Willem van der Veen Avatar answered Sep 21 '22 08:09

Willem van der Veen