every possible pairing in an array javascript in new array

Solutions on MaxInterview for every possible pairing in an array javascript in new array by the best coders in the world

showing results for - "every possible pairing in an array javascript in new array"
Giorgia
01 Jan 2017
1function pairs(arr) {
2    var res = [],
3        l = arr.length;
4    for(var i=0; i<l; ++i)
5        for(var j=i+1; j<l; ++j)
6            res.push([arr[i], arr[j]]);
7    return res;
8}
9pairs([1, 2, 3]).forEach(function(pair){
10    console.log(pair);
11});
12