how can I convert an array like:
[2, 5, 4, 0]
into an array of pairs but not getting every pair. For example from the above array given, I'd like the following array:
[[2, 5],[5, 4], [4, 0], [0, 2]
.
Furthermore, is it possible to have another function that gives back an array like: [[2,5,4],[5,4,0],[4,0,2],[0,2,5]]
?
And also another one that gives back: [[2,5,4,0],[5,4,0,2],[4,0,2,5],[0,2,5,4]]
?
Edit: I have tried the following which gives back every possible pair in the array, but it's not what I'm looking for:
let result = pieces[0].reduce( (acc, v, i) =>
acc.concat(pieces[0].slice(i+1).map( w => [v, w] )),
[]);
This will show: [ [ 2, 5 ], [ 2, 4 ], [ 2, 0 ], [ 5, 4 ], [ 5, 0 ], [ 4, 0 ] ]
Edit2: my problem here is that everything I have tried gives me every permutation of the two dimensional, three dimensional and four dimensional arrays, but I only want the examples above. Let's say, the permutation of consecutive values.