Pairwise an array in Javascript
function pairwise(arr) {
return arr.map(function (item, index, _arr) {
return _arr.map(function (_item) {
if (item != _item) {
return [item, _item];
}
}).filter(Boolean);
});
}
an example:
pairwise(["Cat","Dog","Bird"]);
# outputs:
array(3): {
[0]: array(2): {
[0]: array(2): {
[0]: string(3): "Cat"
[1]: string(3): "Dog"
}
[1]: array(2): {
[0]: string(3): "Cat"
[1]: string(4): "Bird"
}
}
[1]: array(2): {
[0]: array(2): {
[0]: string(3): "Dog"
[1]: string(3): "Cat"
}
[1]: array(2): {
[0]: string(3): "Dog"
[1]: string(4): "Bird"
}
}
[2]: array(2): {
[0]: array(2): {
[0]: string(4): "Bird"
[1]: string(3): "Cat"
}
[1]: array(2): {
[0]: string(4): "Bird"
[1]: string(3): "Dog"
}
}
}