2
Arrayinarray=[];
let options = driver.findElements(By.css("[section='trim'] select"));
options.then(swap=>{
    swap.map((key)=>{
        var s=key.findElements(By.css("option"));
        s.then(mt=>{
            Arrayinarray.push(mt)
        })
    })

});

this is my selenium code.

I have to get each element of the array in correct order

For example.

var Arrayinarray=[[1,2,3],[4,5,6,5],[7,8,9],[1,6,3],[1,5,7][1,2,2],[7,2,9,3]];

Expected output:

1471117 1471112 1471119 1471113 1471127 1471122
1471129 1471123 1471127 1471122 1471129 1471123
...

I have to permutate the combination of this multidimensional array. I placed many for loops and map functions. However, it does not work.

2
  • Please show us your code. Commented Jan 19, 2018 at 12:36
  • 1
    @AyyappanRK No, don't put the code in the comments. Edit your question and put it there.. Commented Jan 19, 2018 at 12:39

2 Answers 2

2

You could take a iterative approach by collecting all part arrays and take the final arrays for getting a number back.

var values = [[1, 2, 3], [4, 5, 6, 5], [7, 8, 9], [1, 6, 3], [1, 5, 7], [1, 2, 2], [7, 2, 9, 3]],
    result = values
        .reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []))
        .map(s => +s.join(''));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

1

The number of results can quickly grow large. In your case it will have 3*4*3*3*3*3*4 results, i.e. 3888 results, but making some of the sub arrays larger, and/or adding more of them multiplies the number of results quickly.

You could use a generator function and then select the first X results from it, or all of them (which I do below):

function * generateCombis(arr) {
    if (arr.length === 1) return yield * arr[0];
    const shift = 10**(arr.length-1);
    for (let val of arr[0]) {
        for (let val2 of generateCombis(arr.slice(1))) yield val*shift+val2
    }
}
// Example
var Arrayinarray=[[1,2,3],[4,5,6,5],[7,8,9],[1,6,3],[1,5,7], [1,2,2],[7,2,9,3]];

const result = Array.from(generateCombis(Arrayinarray));
console.log(result);

Using the spread syntax, you could make the function take the subarrays as separate arguments (instead of taking a nested array), which makes some parts of the code more readable (but that is debatable):

function * generateCombis(current, ...rest) {
    if (!rest.length) return yield * current;
    const shift = 10**rest.length;
    for (let val of current) {
        for (let val2 of generateCombis(...rest)) yield val*shift+val2
    }
}
// Example
var Arrayinarray=[[1,2,3],[4,5,6,5],[7,8,9],[1,6,3],[1,5,7], [1,2,2],[7,2,9,3]];

const result = Array.from(generateCombis(...Arrayinarray));
console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.