简体   繁体   English

如何在Ramda.map中访问迭代索引

[英]How can I access iteration index in Ramda.map

I used to write something like 我曾经写过类似的东西

_.map(items, (item, index) => {});

with lodash. 与lodash。 Usually I don't need index but sometimes it's useful. 通常我不需要index但有时它很有用。

I'm migrating to Ramda now: 我现在要迁移到Ramda:

R.map((item, index) => {}, items);

index is undefined . index undefined Sure, I can create variable index in upper scope and increment it every time in map body but it's kinda wrong from FP point of view that Ramda stands for. 当然,我可以在上限范围内创建变量index ,并每次在map主体中对其进行递增,但是从FP的角度来看,Ramda代表的观点有点不对。 So is there's any build in way of getting iteration index? 那么获取迭代索引是否有任何方法?

Check out addIndex : 查看addIndex

Creates a new list iteration function from an existing one by adding two new parameters to its callback function: the current index, and the entire list. 通过向其回调函数添加两个新参数,从现有函数中创建一个新的列表迭代函数:当前索引和整个列表。

This would turn, for instance, Ramda's simple map function into one that more closely resembles Array.prototype.map. 例如,这会将Ramda的简单map函数变成一个更类似于Array.prototype.map的函数。 Note that this will only work for functions in which the iteration callback function is the first parameter, and where the list is the last parameter. 请注意,这仅适用于以迭代回调函数为第一个参数且列表为最后一个参数的函数。 (This latter might be unimportant if the list parameter is not used.) (如果不使用list参数,则后者可能并不重要。)

Example from the docs: 来自文档的示例:

var mapIndexed = R.addIndex(R.map);
mapIndexed((val, idx) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r']);
//=> ['0-f', '1-o', '2-o', '3-b', '4-a', '5-r']

You can also use mapIndexed from Ramda Adjunct which uses R.addIndex under the hood. 您还可以使用Ramda Adjunct中的R.addIndex ,该引擎在R.addIndex使用R.addIndex

R.map function that more closely resembles Array.prototype.map. R.map函数与Array.prototype.map非常相似。 It takes two new parameters to its callback function: the current index, and the entire list. 它的回调函数有两个新参数:当前索引和整个列表。

RA.mapIndexed((val, idx, list) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r']);
//=> ['0-f', '1-o', '2-o', '3-b', '4-a', '5-r']

It also offsers a reduceIndexed 它还提供了reduceIndexed

const initialList = ['f', 'o', 'o', 'b', 'a', 'r'];

reduceIndexed((acc, val, idx, list) => acc + '-' + val + idx, '', initialList);
//=> "-f0-o1-o2-b3-a4-r5"

As an alternative to addIndex you could use toPairs before mapping, from the documentation : 作为addIndex的替代方法,您可以在映射之前使用toPairs ,来自文档

Converts an object into an array of key, value arrays. 将对象转换为键,值数组的数组。 Only the object's own properties are used. 仅使用对象自身的属性。 Note that the order of the output array is not guaranteed to be consistent across different JS platforms. 注意,不能保证输出数组的顺序在不同的JS平台之间是一致的。

The documentation talks only about objects but it works equally well with arrays. 该文档仅讨论对象,但与数组同样适用。 In your example: 在您的示例中:

R.map(([index, item]) => {}, R.toPairs(items));

// or, equivalent:

R.compose(
    R.map(([index, item]) => {}),
    R.toPairs,
)(items)

Bear in mind that in each index/value pair the index is always the first element, so the order is reversed compared to lodash (or the native Array.prototype.map ). 请记住,在每个索引/值对中,索引始终是第一个元素,因此与lodash(或本地Array.prototype.map )相比,顺序是相反的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM