0

Hello stackoverflow members.

I come with the following problem:

To start we have

var myArray = [[array1],[array2],[array3],[arrayN],...];

where each array is filled with a known number of strings such as

var array1 = ["a","b"], array2 = ["1","2"], array3=["&","é"];....

and so on.

I'm looking for a method to get this result:

expected result ===> a1&;a1é;a2&;a2é;b1&;b1é;b2&;b2é; ....

If the number of dimension were fixed I could use for loops to iterate and build the result, BUT here the problem is that I want to be able to enter N arrays in the main array myArray and by doing so, I change the depth of the nested loops.

If not do you have some ideas to put me on the track of the solution to this?

Thanks!

EDIT by the way this is what i experimented:

for (i=0; i<myArray[0].length; i++){
    for (var j =0; j<myArray[1].length; i++){
        for(var k = 0; k<myArray[2].length; k++{
            console.log(i+j+k);
        }
    }
}

BTW i can't find a way to describe a function which would nest N for loops where N is myArray.length + 1 (the number of arrays in myArray).

EDIT: i found an iterative way of doing it and wanted to share the solution:JSFiddle

sp3
  • 74
  • 1
  • 9
  • _"Is there a library to achieve this quicly?"_ Asking for library recommendations is specifically offtopic on Stack Overflow - you may want to rephrase that part of your question, and perhaps include any research /code you've tried to solve this yourself so far. – James Thorpe Nov 02 '15 at 17:26
  • Sorry what exactly is the issue? No matter _how many_ nested arrays you have, you can easily recursively loop through them because all of them have a `length` property, and none are of unknown length. Its just a matter of appending the contents to a global string and done. – somethinghere Nov 02 '15 at 17:28
  • So you loop over the array....not sure what is hard – epascarello Nov 02 '15 at 17:30
  • 1
    If you want to "zip" them, please see http://stackoverflow.com/questions/4856717/javascript-equivalent-of-pythons-zip-function – deamentiaemundi Nov 02 '15 at 17:32
  • @JamesThorpe I just noticed it i'll correct. – sp3 Nov 02 '15 at 18:34
  • @deamentiaemundi It's not "just" a zip : i want to get EACH possibility not make groups with the first value of each array then the second value etc... – sp3 Nov 02 '15 at 18:36
  • @somethinghere i guess it is what i am looking for : recursivity. However i do not know how to do it and the MDN docs about recursivity are not really clear to me. – sp3 Nov 02 '15 at 19:24

1 Answers1

1

To get a flat list of all cells, something like the following recursive function should work (if you have a non-empty array of arrays, and all array items are strings):

function makeFlatList(inputArray) {
  if (inputArray.length == 1) {                         // if this array has only one array inside
    return inputArray[0];                               // return the array inside
  } else {
    var outArr = [];
    var arrayShifted = inputArray.slice(1);             // remove first subarray from inputarray
    var arrayShiftedFlat = makeFlatList(arrayShifted);  // recursive call
    for (var i=0; i<inputArray[0].length ; i++) {       // loop over first array
      for (var j=0; j<arrayShiftedFlat.length; j++) {
        outArr.push(inputArray[0][i]+arrayShiftedFlat[j]);  // add items to outArr  
      }
    }
    return outArr;
  }
}

Working JSBin here

wintvelt
  • 13,855
  • 3
  • 38
  • 43
  • Make it into a working SO snippet to see if it does what is says on the tin and this is an excellent answer. – somethinghere Nov 02 '15 at 19:59
  • This is totally what i was looking for, that's amazing! Thanks for your help! I'll study it deeper tonight to get a better understanding of it and if i find an iterative way of doing it i'll provide it too. Thank you a lot!! BTW is it supposed to be a trivial question? – sp3 Nov 02 '15 at 20:46
  • 1
    For future reference, SO has its own snippet feature, it's the document icon with the `<>` in it. +1 – somethinghere Nov 02 '15 at 20:47
  • @somethinghere Thanks for pointing out SO code snippet, looks useful for HTML+CSS+javascript, but less useful for pure javascript + console output, and it lacks fiddle feature (which would allow OP to edit snippet and see results). (upvoted your tip nonetheless ;) – wintvelt Nov 02 '15 at 20:55
  • Don't know if question was trivial, just liked the puzzle ;) If you read through @daementiaemundi's link, you will find in the answers there some related solutions. Looks like JavaScript-variants of Python zip function could also be a solution. So +1 there.. – wintvelt Nov 02 '15 at 23:24