Skip to content Skip to sidebar Skip to footer

Create Array With All Unique Combinations

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 array with all

Solution 1:

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;
        }
    }
}

Post a Comment for "Create Array With All Unique Combinations"