admin管理员组文章数量:1129773
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?
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?
4 Answers
Reset to default 1062You 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:
- currentValue
The current element being processed in the array.2) index
The index of the current element being processed in the array.
- array
The array map was called upon.
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 thethis
value for the callback function. Keep in mind that you have to use the regularfunction
keyword in order to declare the callback since an arrow function doesn't have its own binding to thethis
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);
- suppose you have an array like
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr.map((myArr, index) => {
console.log(`your index is -> ${index} AND value is ${myArr}`);
})
> output will be
index is -> 0 AND value is 1
index is -> 1 AND value is 2
index is -> 2 AND value is 3
index is -> 3 AND value is 4
index is -> 4 AND value is 5
index is -> 5 AND value is 6
index is -> 6 AND value is 7
index is -> 7 AND value is 8
index is -> 8 AND value is 9
Using Ramda:
import {addIndex, map} from 'ramda';
const list = [ 'h', 'e', 'l', 'l', 'o'];
const mapIndexed = addIndex(map);
mapIndexed((currElement, index) => {
console.log("The current iteration is: " + index);
console.log("The current element is: " + currElement);
console.log("\n");
return 'X';
}, list);
本文标签: javascriptIndex inside map() functionStack Overflow
版权声明:本文标题:javascript - Index inside map() function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736701756a1948459.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
map
is supposed to preserve the structure of the array that is, only its values should be transformed not the array itself. – user6445533 Commented Jul 14, 2016 at 6:12