var arr = ["cat","dog","chicken","pig"].map(function(item,i,arr) { return arr.map(function(_item) { if( item != _item) return [item, _item];}); });
This will return the expected results. There are warnings, it does not work in older browsers without padding.
Also, the duplicate value is "undefined" instead of 4 arrays of 3. I am sure there is a more elegant way to handle this.
Array.prototype.map () - MDN
edit
this will give you the correct pairing combinations.
var arr = ["cat","dog","chicken","pig"].map(function(item,i,arr) { var tmp = arr.map(function(_item) { if( item != _item) return [item, _item];}); return tmp.splice(tmp.indexOf(undefined),1), tmp; });
Array Splicing Method - MDN
and here is a more readable version of the same code.
var myArray = ["cat", "dog", "chicken", "pig"]; var pairwise = myArray.map(function(item, index, originalArray) { var tmp = originalArray.map(function(_item) { if (item != _item) { return [item, _item]; } }); tmp.splice(tmp.indexOf(undefined), 1);