How to find index of all occurrences of an element in array (Ramda.js way)?

qaraluch

I am trying to find index of all the instances let's say Header and Footer in an array.

var arr = [
'data',
'data',
'data',
'data',
'Header',
'data',
'data',
'data',
'Footer',
'data',
'Header',
'data',
'Footer',
'data'
];

I know how to do this in plain JS (How to find index of all occurrences of element in array?), but I wonder how it would be done in FP, ramda.js in particular?

I know how to do this for first instance R.findIndex(R.test(_regexForHeader)) but can not wrap my head around looping through all array. Thanks for help.

Scott Christopher

@pierrebeitz's answer is on point, namely that when you need access to indices while iterating over a list you'll typically zip the list up along with its indices. Ramda provides an R.addIndex function for modifying functions like map to provide the index along with each element while iterating.

The nesting in his example can also be replaced with a composition pipeline if you prefer:

const zipWithIndex = addIndex(map)(pair);

const isHeaderOrFooter = either(equals('Header'), equals('Footer'));

const hfIndices = pipe(
  zipWithIndex,
  filter(pipe(head, isHeaderOrFooter)),
  map(nth(1))
);

hfIndices(arr);

One thing to be mindful of with both of these approaches is you'll end up iterating over the list multiple times. This won't typically be a problem for a small list, however for larger lists you might want to consider using R.into which effectively fuses the maps and filter together into a transducer that will now take only a single pass over the list (see http://simplectic.com/blog/2015/ramda-transducers-logs/ for a good intro to transducers).

This can be achieved with a small tweak to hfIndices in the above example by swapping the composition from pipe to compose (transducer functions compose in the opposite order) and wrapping it with into.

const hfIndices = into([], compose(
  zipWithIndex,
  filter(pipe(head, isHeaderOrFooter)),
  map(nth(1))
));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find index of all occurrences of an element in array (Ramda.js way)?

From Java

How to find index of all occurrences of element in array?

From Java

How to find all occurrences of an element in a list

From Dev

How to find index of an Array element in OCaml

From Java

How to find all occurrences of a substring?

From Dev

Most efficient way to insert an element into sorted array and find its index

From Dev

Find all occurrences (letters) in array (word)

From Dev

Find the index of an element in an array created by toArray in JS/JQuery

From Dev

When a draw occurs when tracking most occurrences in a list how to find element with highest index?

From Dev

how to find a element in a nested array and get its sub array index

From Dev

How to find and replace all occurrences of a substring in a string?

From Dev

How to Find all occurrences of a Substring in C

From Dev

How to Find all occurrences of a Substring in C

From Dev

How do I find the index of an element in an array, vector or slice?

From Dev

How to use ng-repeat $index to find element in array

From Dev

How to find index of the last non-empty element in a cell array

From Dev

Java: How to find index of last element of partially initialized sorted array

From Dev

How to find the index of last element which is non-zero in array

From Dev

How to find the index of an element in a multidimensional array by JavaScript/JQuery

From Dev

Find the occurrences (count) of all elements at all positions in an array in Postgres?

From Dev

Find the occurrences (count) of all elements at all positions in an array in Postgres?

From Dev

How to count occurrences of an element in a Swift array?

From Dev

find index of array element in another array javascript

From Dev

find index of array element in another array javascript

From Dev

Java - Use binarySearch to find # of occurrences of certain element in array, without If

From Dev

Understanding how the JS engine is looping through to get the index of an array element

From Dev

How to find element in 2-dimensional array in underscore.js?

From Dev

How to find index of a struct element as index of byte array in union (in standard C)

From Dev

how I can print all case element in array by js

Related Related

  1. 1

    How to find index of all occurrences of an element in array (Ramda.js way)?

  2. 2

    How to find index of all occurrences of element in array?

  3. 3

    How to find all occurrences of an element in a list

  4. 4

    How to find index of an Array element in OCaml

  5. 5

    How to find all occurrences of a substring?

  6. 6

    Most efficient way to insert an element into sorted array and find its index

  7. 7

    Find all occurrences (letters) in array (word)

  8. 8

    Find the index of an element in an array created by toArray in JS/JQuery

  9. 9

    When a draw occurs when tracking most occurrences in a list how to find element with highest index?

  10. 10

    how to find a element in a nested array and get its sub array index

  11. 11

    How to find and replace all occurrences of a substring in a string?

  12. 12

    How to Find all occurrences of a Substring in C

  13. 13

    How to Find all occurrences of a Substring in C

  14. 14

    How do I find the index of an element in an array, vector or slice?

  15. 15

    How to use ng-repeat $index to find element in array

  16. 16

    How to find index of the last non-empty element in a cell array

  17. 17

    Java: How to find index of last element of partially initialized sorted array

  18. 18

    How to find the index of last element which is non-zero in array

  19. 19

    How to find the index of an element in a multidimensional array by JavaScript/JQuery

  20. 20

    Find the occurrences (count) of all elements at all positions in an array in Postgres?

  21. 21

    Find the occurrences (count) of all elements at all positions in an array in Postgres?

  22. 22

    How to count occurrences of an element in a Swift array?

  23. 23

    find index of array element in another array javascript

  24. 24

    find index of array element in another array javascript

  25. 25

    Java - Use binarySearch to find # of occurrences of certain element in array, without If

  26. 26

    Understanding how the JS engine is looping through to get the index of an array element

  27. 27

    How to find element in 2-dimensional array in underscore.js?

  28. 28

    How to find index of a struct element as index of byte array in union (in standard C)

  29. 29

    how I can print all case element in array by js

HotTag

Archive