2

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.

Raúl
  • 159
  • 1
  • 10
  • 2
    Hello, what did you try? Please show a [mcve]. – Amessihel Mar 10 '22 at 09:52
  • 1
    It's totally possible and not really hard. It's more of a logic problem, requiring some thinking. You should explain what you tried and show some code – aymcg31 Mar 10 '22 at 09:52
  • 1
    Does this answer your question? [Permutations in JavaScript?](https://stackoverflow.com/questions/9960908/permutations-in-javascript) – evolutionxbox Mar 10 '22 at 09:52
  • Thanks @evolutionxbox, that question would solve the last part getting the permutations of the array, but not the first two. But thank you so much! – Raúl Mar 10 '22 at 09:55
  • @Raúl [this answer](https://stackoverflow.com/a/64904542/989920) literally solves your issue – evolutionxbox Mar 10 '22 at 09:56
  • @evolutionxbox no, it doesn't because I don't want avery possible permutation in the two dimensional and three dimensional arrays, only the consecutive values. For example, that solution would give me: `[ [ 2, 5 ], [ 2, 4 ], [ 2, 0 ], [ 5, 4 ], [ 5, 0 ], [ 4, 0 ] ]` and I want just `[[2, 5],[5, 4], [4, 0], [0, 2]` – Raúl Mar 10 '22 at 10:00
  • Ah. thank you @Raúl for the correction – evolutionxbox Mar 10 '22 at 10:01
  • @evolutionxbox no problem, thank you for giving me possible solutions. My problem here is I only want some permutations and everything I have tried gives me every permutation, not just the ones I want – Raúl Mar 10 '22 at 10:03

2 Answers2

3

I believe this is what you are looking for:

let arr = [2, 5, 4, 0]
function getPermutation(chunk){
    let result = arr.reduce((acc,e,i) => {
        if(i + chunk <= arr.length)
            acc.push(arr.slice(i,i+chunk))
        else
            acc.push(arr.slice(i,i+chunk).concat(arr.slice(0,(i+chunk) % arr.length)))
        return acc;
    },[])
    return result 
}

console.log(getPermutation(2))
console.log(getPermutation(3))
console.log(getPermutation(4))
Alan Omar
  • 4,023
  • 1
  • 9
  • 20
1

Here is one way to do it:

const input = [2, 5, 4, 0];

const output = [];

for(let i=0; i<input.length - 1; i++) {
    output.push([input[i], input[i+1]]);
}
output.push([input[input.length-1], input[0]]);

console.log(output);
Ahmad
  • 12,336
  • 6
  • 48
  • 88
  • Yes, but that would not give me the last pair `[0,2]`, I mean getting the last element and then the first one. Thank you! – Raúl Mar 10 '22 at 09:56
  • @Raúl updated... – Ahmad Mar 10 '22 at 10:06
  • thank you so much! Exactly what I was looking for the two dimensional array. Is there a way of doing another function for the two other problems I propose, the three dimensional and four dimensional? – Raúl Mar 10 '22 at 10:12
  • Thank you for your solution, but @alan-omar solution was more complete to what I was looking for – Raúl Mar 10 '22 at 10:23