先来看看要实现什么样的效果

var data = [[1,2,3],[4,5,6]];

var result =  data.group();

// 结果:[1,4] [1,5], [1,6],[2,4],[2,5],[2,6]....以此类推;

【源码】扩展Array方法:

Array.prototype.group = function() {
    var $this = this;
    var result = new Array();
    var findNext = function next(currentIndex, arr) {
        var item = $this[currentIndex];
        var tempArr = arr;
        var index = currentIndex;
        for (var k = 0; k < item.length; k++) {
            if (!arr) tempArr = new Array();
            tempArr[tempArr.length] = item[k];
            if (tempArr.length == $this.length) {
                result[result.length] = tempArr;
                tempArr = arr.slice();
                tempArr.length = tempArr.length - 1;
                continue;
            }
            index++;
            if (index < $this.length && next(index, tempArr)) {
                index--; // 恢复递归之前的索引
                tempArr = arr ? arr.slice() : new Array();
                tempArr.length = tempArr.length > 0 ? tempArr.length - ($this.length - index) : 0;
            }
        }
        return true;
    };

    findNext(0);

    return result;
}