Create array with all unique combinations

前端 未结 1 1417
借酒劲吻你
借酒劲吻你 2021-01-27 19:57

Can\'t figure out how to create an array with all matches. I suppose I need a recursive function for this.

I like to get all values from the JSON below and create an ar

相关标签:
1条回答
  • 2021-01-27 20:26
    var indices = [];
    var lengths = [];
    for (i = 0; i<models.length; i++) {
        indices[i] = 0;
        lengths[i] = models[i].values.length;
    }
    var matches = [];
    while (indices[0] < lengths[0]) {
        var row = [];
        for (i = 0; i<indices.length; i++) {
           row.push(models[i].values[indices[i]]);
        }
        matches.push(row);
        /* Cycle the indexes */
        for (j = indices.length-1; j >= 0; j--) {
            indices[j]++;
            if (indices[j] >= lengths[j] && j != 0) {
                indices[j] = 0;
            } else {
                break;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题