Javascript map() 函数内的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38364400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:23:04  来源:igfitidea点击:

Index inside map() function

javascriptfunctional-programmingimmutable.js

提问by Zygimantas

I am missing a option how to get the index number inside the mapfunction using Listfrom Immutable.js:

我缺少如何map使用Listfrom获取函数内部索引号的选项Immutable.js

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

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

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

文档显示的是map()回报Iterable<number, M>。有什么优雅的方式可以满足我的需求吗?

回答by Samuel Toh

You will be able to get the current iteration's indexfor the mapmethod through its 2nd parameter.

您将能够通过其第二个参数index获取该map方法的当前迭代。

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

另见: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.

参数

callback - 生成新数组元素的函数,采用三个参数:

1) currentValue
数组中正在处理的当前元素。

2) index
当前正在处理的元素在数组中的索引。

3) array
调用了数组映射。

回答by Willem van der Veen

Array.prototype.map()index:

Array.prototype.map()指数:

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

可以Array.prototype.map()通过回调函数的第二个参数访问索引。下面是一个例子:

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():

的其他参数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 thisvalue for the callback function. Keep in mind that you have to use the regular functionkeywordin order to declare the callback since an arrow function doesn't have its own binding to the thiskeyword.
  • 回调函数的第三个参数公开了调用 map 的数组
  • 的第二个参数Array.map()是一个对象,它将作为this回调函数的值。请记住,您必须使用常规function关键字来声明回调,因为箭头函数没有自己的this关键字绑定。

For example:

例如:

const array = [1, 2, 3, 4];

const thisObj = {prop1: 1}


const map = array.map( function (x, index, array) {
  console.log(array);
  console.log(this)
}, thisObj);

回答by David

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);