Skip to content
Advertisement

javascript: return every possible pairs in array

the title explains it all

somehow I’d like to use the method “Combination” that math has, this is the Wikipedia page to be clear: https://en.wikipedia.org/wiki/Combination

I have already found the solution with two loops, I want to do it in one loop

example:

JavaScript

Advertisement

Answer

You can use Array.flatMap() to iterate the array, and Array.map() to iterate all items after the current (by slicing from index + 1), and return the pair.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement