0

If I have multiple arrays (I dont know how many, or their length)

arr1 = ["a","b","c"];
arr2 = ["red","green"];
arr3 = ["10","11","12","13"];

And I would like to have something like

res = [ 
          ["a","red","10"], ["a","red","11"],
          ["a","red","12"],["a","red","13"],
          ["a","green","10"],["a","green","11"], 
          ...   
     ]; 

You know, combine them... I dont know the number of arrays or the length.. And the final result be One array with all possible combinations.

4You
  • 41
  • 1
  • 4
  • Will they all have the same length? – Tuvia Apr 18 '16 at 20:08
  • If you want a multidimensional array just create a new array and push each sub array into it. `var arr = []; arr.push(arr1); arr.push(arr2);` etc. If you can do this programmatically depends on how you get your arrays in the first place. – ste2425 Apr 18 '16 at 20:10
  • 1
    This looks a bit more complicated than those suggestions, he needs every combination of every array element. – IrkenInvader Apr 18 '16 at 20:11
  • Every array content is a Kind of information the User gave me. So I really dont know how many "type" of content he will give. Also how many itens in each content.. So they will not be the same length.. And They will not be 3 arrays.. they can be many arrays.. maybe 50 arrays.. maybe not.. – 4You Apr 18 '16 at 20:12
  • @IrkenInvader Good point i missed that in his example data. – ste2425 Apr 18 '16 at 20:12
  • how does the user gave you the array content? What is the initial state? 1 array? No array? – fbid Apr 18 '16 at 20:16
  • Is there anything constant at all about the amount that appears in each array? You just used the example of an array with 3 elements, 2 elements, and 4 elements. Is there a set number to be expected for anything in this situation of yours? Surely there must be some form of limit you want to impose here. Unless a user entering thousands of items for a single array is ok with you. – JoeL Apr 18 '16 at 20:18
  • I made a javascript they put the product inventory. So he inserts.. "Shoes"-> "red"->"size10". "Shoes"->"blue"->"size11". And In the final.. I will show "count" the number of combinations.. including "Zero". So I need to show.. "Shoes"->"blue"->"size10" even if he didnt put the information, cause that Combination is possible. I am already saving each new "attribute" as array like my example. So I just need to combine them. In this example the product was Shoes.. but It could be any other product with different kind of attributes and combinations.. – 4You Apr 18 '16 at 20:20
  • Amazing @IrkenInvader !!! Thank you very much :) it does exactly what I need.. – 4You Apr 18 '16 at 20:29

1 Answers1

-2

You should be able to use the array method reduce to create a single array.

/** @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#Examples */

var arr1 = ["a", "b", "c"],
  arr2 = ["red", "green"],
  arr3 = ["10", "11", "12", "13"],
  flattened = [arr1, arr2, arr3].reduce(function(a, b) {
    return a.concat(b);
  }, []);

alert(flattened);
colecmc
  • 3,133
  • 2
  • 20
  • 33