【问题标题】:Index inside map() functionmap() 函数内的索引
【发布时间】:2016-11-16 19:00:48
【问题描述】:

我缺少一个选项,如何使用来自Immutable.jsList 获取map 函数中的索引号:

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

Documentation shows 表示 map() 返回 Iterable<number, M>。有什么优雅的方式可以满足我的需要吗?

【问题讨论】:

  • 请记住,map 应该保留数组的结构,也就是说,应该只转换其值而不是数组本身。
  • Array.prototype.map() 使用回调函数作为转换创建一个新数组

标签: javascript functional-programming immutable.js


【解决方案1】:

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

示例:

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

输出:

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

另请参阅: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/map

参数

回调 - 产生新数组元素的函数,接受三个参数:

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

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

3) 数组
调用了数组映射。

【讨论】:

  • 地图的回调函数是不是应该一直有return语句?您的代码中的“X”是什么意思?
  • @HarshKanchina map 操作用于通过遍历给定数组的元素来构造新数组。要回答您的问题,是的,需要一个 return 语句,对于这种情况,它在每次迭代时返回值“X”。因此代码的最终结果将是[ 'X', 'X','X','X' ]
  • @But 'X' 没有在任何地方定义。那么它指的是什么?函数怎么知道这里的 X 指的是什么?
  • @HarshKanchina 'X' 是一个字符串。
  • 我希望这个索引从 1 开始,我怎样才能做到这一点?
【解决方案2】:

Array.prototype.map()索引:

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

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

const map = array.map((x, index) => {
  console.log(index);
  return x + index;
});

console.log(map);

Array.prototype.map()的其他参数:

  • 回调函数的第三个参数公开了调用 map 的数组
  • Array.map() 的第二个参数是一个对象,它将是回调函数的this 值。请记住,您必须使用常规的function 关键字 来声明回调,因为箭头函数没有自己绑定到this 关键字。

例如:

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

const thisObj = { prop1: 1 }

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

【讨论】:

    【解决方案3】:
    • 假设你有一个类似的数组

       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
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2022-11-22
        • 1970-01-01
        • 2021-03-24
        • 1970-01-01
        • 1970-01-01
        • 2014-12-09
        • 2014-08-30
        • 2021-04-04
        • 2013-07-16
        相关资源
        最近更新 更多