issue with extracting combination of values from multiple nested arrays










2















trying to iterate over multiple nested arrays to get every single combination of possible values in a new array.



example:



[
['a1', 'a2'],
['b1', 'b2'],
['c1', 'c2']
]


output:



[
['a1'],
['a2'],
['b1'],
['b2'],
['c1'],
['c2'],
['a1', 'b1'],
['a1', 'b2'],
['a1', 'c1'],
['a1', 'c2'],
['a2', 'b1'],
['a2', 'b2'],
['a2', 'c1'],
['a2', 'c2'],
['b1', 'c1'],
['b1', 'c2'],
['b2', 'c1'],
['b2', 'c2']
]


any idea how I can achieve this, perhaps I need to split arrays in the first place ?










share|improve this question






















  • Do you want every single combination or only combinations with 1 and 2 elements in them?

    – VLAZ
    Nov 15 '18 at 12:17











  • I think you should 1. Post any attempts of code that you've written and then ask about a specific problem that you're having, and 2. Explain in a little more detail as to how your input maps to that output

    – Khauri McClain
    Nov 15 '18 at 12:19











  • Just clarifying: so any combinations with 1..N members.

    – VLAZ
    Nov 15 '18 at 12:19






  • 1





    You have this similar question but i'm not flagging as duplicate because i'm not sure I understand the logic - why does the first 6 elements in the output are valid, but not ['b1', 'a1']

    – Alon Eitan
    Nov 15 '18 at 12:20











  • I need exactly as above, not all combinations as ['a1', 'b1'] and ['b1', 'a1']

    – user1751287
    Nov 15 '18 at 12:21
















2















trying to iterate over multiple nested arrays to get every single combination of possible values in a new array.



example:



[
['a1', 'a2'],
['b1', 'b2'],
['c1', 'c2']
]


output:



[
['a1'],
['a2'],
['b1'],
['b2'],
['c1'],
['c2'],
['a1', 'b1'],
['a1', 'b2'],
['a1', 'c1'],
['a1', 'c2'],
['a2', 'b1'],
['a2', 'b2'],
['a2', 'c1'],
['a2', 'c2'],
['b1', 'c1'],
['b1', 'c2'],
['b2', 'c1'],
['b2', 'c2']
]


any idea how I can achieve this, perhaps I need to split arrays in the first place ?










share|improve this question






















  • Do you want every single combination or only combinations with 1 and 2 elements in them?

    – VLAZ
    Nov 15 '18 at 12:17











  • I think you should 1. Post any attempts of code that you've written and then ask about a specific problem that you're having, and 2. Explain in a little more detail as to how your input maps to that output

    – Khauri McClain
    Nov 15 '18 at 12:19











  • Just clarifying: so any combinations with 1..N members.

    – VLAZ
    Nov 15 '18 at 12:19






  • 1





    You have this similar question but i'm not flagging as duplicate because i'm not sure I understand the logic - why does the first 6 elements in the output are valid, but not ['b1', 'a1']

    – Alon Eitan
    Nov 15 '18 at 12:20











  • I need exactly as above, not all combinations as ['a1', 'b1'] and ['b1', 'a1']

    – user1751287
    Nov 15 '18 at 12:21














2












2








2


2






trying to iterate over multiple nested arrays to get every single combination of possible values in a new array.



example:



[
['a1', 'a2'],
['b1', 'b2'],
['c1', 'c2']
]


output:



[
['a1'],
['a2'],
['b1'],
['b2'],
['c1'],
['c2'],
['a1', 'b1'],
['a1', 'b2'],
['a1', 'c1'],
['a1', 'c2'],
['a2', 'b1'],
['a2', 'b2'],
['a2', 'c1'],
['a2', 'c2'],
['b1', 'c1'],
['b1', 'c2'],
['b2', 'c1'],
['b2', 'c2']
]


any idea how I can achieve this, perhaps I need to split arrays in the first place ?










share|improve this question














trying to iterate over multiple nested arrays to get every single combination of possible values in a new array.



example:



[
['a1', 'a2'],
['b1', 'b2'],
['c1', 'c2']
]


output:



[
['a1'],
['a2'],
['b1'],
['b2'],
['c1'],
['c2'],
['a1', 'b1'],
['a1', 'b2'],
['a1', 'c1'],
['a1', 'c2'],
['a2', 'b1'],
['a2', 'b2'],
['a2', 'c1'],
['a2', 'c2'],
['b1', 'c1'],
['b1', 'c2'],
['b2', 'c1'],
['b2', 'c2']
]


any idea how I can achieve this, perhaps I need to split arrays in the first place ?







javascript arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 12:16









user1751287user1751287

193114




193114












  • Do you want every single combination or only combinations with 1 and 2 elements in them?

    – VLAZ
    Nov 15 '18 at 12:17











  • I think you should 1. Post any attempts of code that you've written and then ask about a specific problem that you're having, and 2. Explain in a little more detail as to how your input maps to that output

    – Khauri McClain
    Nov 15 '18 at 12:19











  • Just clarifying: so any combinations with 1..N members.

    – VLAZ
    Nov 15 '18 at 12:19






  • 1





    You have this similar question but i'm not flagging as duplicate because i'm not sure I understand the logic - why does the first 6 elements in the output are valid, but not ['b1', 'a1']

    – Alon Eitan
    Nov 15 '18 at 12:20











  • I need exactly as above, not all combinations as ['a1', 'b1'] and ['b1', 'a1']

    – user1751287
    Nov 15 '18 at 12:21


















  • Do you want every single combination or only combinations with 1 and 2 elements in them?

    – VLAZ
    Nov 15 '18 at 12:17











  • I think you should 1. Post any attempts of code that you've written and then ask about a specific problem that you're having, and 2. Explain in a little more detail as to how your input maps to that output

    – Khauri McClain
    Nov 15 '18 at 12:19











  • Just clarifying: so any combinations with 1..N members.

    – VLAZ
    Nov 15 '18 at 12:19






  • 1





    You have this similar question but i'm not flagging as duplicate because i'm not sure I understand the logic - why does the first 6 elements in the output are valid, but not ['b1', 'a1']

    – Alon Eitan
    Nov 15 '18 at 12:20











  • I need exactly as above, not all combinations as ['a1', 'b1'] and ['b1', 'a1']

    – user1751287
    Nov 15 '18 at 12:21

















Do you want every single combination or only combinations with 1 and 2 elements in them?

– VLAZ
Nov 15 '18 at 12:17





Do you want every single combination or only combinations with 1 and 2 elements in them?

– VLAZ
Nov 15 '18 at 12:17













I think you should 1. Post any attempts of code that you've written and then ask about a specific problem that you're having, and 2. Explain in a little more detail as to how your input maps to that output

– Khauri McClain
Nov 15 '18 at 12:19





I think you should 1. Post any attempts of code that you've written and then ask about a specific problem that you're having, and 2. Explain in a little more detail as to how your input maps to that output

– Khauri McClain
Nov 15 '18 at 12:19













Just clarifying: so any combinations with 1..N members.

– VLAZ
Nov 15 '18 at 12:19





Just clarifying: so any combinations with 1..N members.

– VLAZ
Nov 15 '18 at 12:19




1




1





You have this similar question but i'm not flagging as duplicate because i'm not sure I understand the logic - why does the first 6 elements in the output are valid, but not ['b1', 'a1']

– Alon Eitan
Nov 15 '18 at 12:20





You have this similar question but i'm not flagging as duplicate because i'm not sure I understand the logic - why does the first 6 elements in the output are valid, but not ['b1', 'a1']

– Alon Eitan
Nov 15 '18 at 12:20













I need exactly as above, not all combinations as ['a1', 'b1'] and ['b1', 'a1']

– user1751287
Nov 15 '18 at 12:21






I need exactly as above, not all combinations as ['a1', 'b1'] and ['b1', 'a1']

– user1751287
Nov 15 '18 at 12:21













3 Answers
3






active

oldest

votes


















3














You could take an recursive approach by handing over the next indices and collect the temporary arrays.






function getCombinations(array, max) 

function iter(i, j, temp)
if (array[i] && j >= array[i].length)
j = 0;
i++;

if (!array[i]

var result = ;
iter(0, 0, );
return result;


var array = [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']],
result = getCombinations(array, 2);

console.log(result.map(a => a.join(' ')));

.as-console-wrapper max-height: 100% !important; top: 0; 








share|improve this answer
































    3














    You could create recursive function using two nested for loops and one parameter to keep current row iteration count so you can start next for loop from there.






    const data = [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']]
    const res =

    function comb(data, n = 0, prev = )
    for (var i = n; i < data.length; i++)
    for (var j = 0; j < data[i].length; j++)
    let el = data[i][j]
    let arr = prev.concat(el);
    if (arr.length <= data[i].length) res.push(arr)
    comb(data, i + 1, arr)




    comb(data)
    console.log(JSON.stringify(res))








    share|improve this answer























    • nice idea with nested loops.

      – Nina Scholz
      Nov 15 '18 at 14:00











    • @Nina Scholz Thank you, not sure if it works correctly in other scenarios with different array lengths.

      – Nenad Vracar
      Nov 15 '18 at 14:01











    • i took a limit for max and exit the recursion.

      – Nina Scholz
      Nov 15 '18 at 14:02


















    3














    If you need the order too (so single elements first, pairs last), that can be done too, just it is going to be a bit longer than the other solutions:






    var org=[['a1', 'a2'],['b1', 'b2'],['c1', 'c2']];

    var res=;

    // singles
    org.forEach(arr =>
    arr.forEach(elem =>
    res.push([elem]);
    );
    );

    // pairs
    var start=0;
    var end=res.length;
    org.forEach(arr =>
    start+=arr.length;
    arr.forEach(elem =>
    for(var i=start;i<end;i++)
    res.push([elem,res[i][0]]);
    );
    );

    console.log(JSON.stringify(res));





    //singles part is a nested pair of loops, pushing all the elements, and //pairs traverses the original array again, just it makes use of the already "flattened" result, end simply keeps the number of single elements (as the length of the array will grow) and start always jumps to the beginning of the next sub-array (so 'a1','a2'-like pairs are not generated, as it was required).






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319321%2fissue-with-extracting-combination-of-values-from-multiple-nested-arrays%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      You could take an recursive approach by handing over the next indices and collect the temporary arrays.






      function getCombinations(array, max) 

      function iter(i, j, temp)
      if (array[i] && j >= array[i].length)
      j = 0;
      i++;

      if (!array[i]

      var result = ;
      iter(0, 0, );
      return result;


      var array = [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']],
      result = getCombinations(array, 2);

      console.log(result.map(a => a.join(' ')));

      .as-console-wrapper max-height: 100% !important; top: 0; 








      share|improve this answer





























        3














        You could take an recursive approach by handing over the next indices and collect the temporary arrays.






        function getCombinations(array, max) 

        function iter(i, j, temp)
        if (array[i] && j >= array[i].length)
        j = 0;
        i++;

        if (!array[i]

        var result = ;
        iter(0, 0, );
        return result;


        var array = [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']],
        result = getCombinations(array, 2);

        console.log(result.map(a => a.join(' ')));

        .as-console-wrapper max-height: 100% !important; top: 0; 








        share|improve this answer



























          3












          3








          3







          You could take an recursive approach by handing over the next indices and collect the temporary arrays.






          function getCombinations(array, max) 

          function iter(i, j, temp)
          if (array[i] && j >= array[i].length)
          j = 0;
          i++;

          if (!array[i]

          var result = ;
          iter(0, 0, );
          return result;


          var array = [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']],
          result = getCombinations(array, 2);

          console.log(result.map(a => a.join(' ')));

          .as-console-wrapper max-height: 100% !important; top: 0; 








          share|improve this answer















          You could take an recursive approach by handing over the next indices and collect the temporary arrays.






          function getCombinations(array, max) 

          function iter(i, j, temp)
          if (array[i] && j >= array[i].length)
          j = 0;
          i++;

          if (!array[i]

          var result = ;
          iter(0, 0, );
          return result;


          var array = [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']],
          result = getCombinations(array, 2);

          console.log(result.map(a => a.join(' ')));

          .as-console-wrapper max-height: 100% !important; top: 0; 








          function getCombinations(array, max) 

          function iter(i, j, temp)
          if (array[i] && j >= array[i].length)
          j = 0;
          i++;

          if (!array[i]

          var result = ;
          iter(0, 0, );
          return result;


          var array = [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']],
          result = getCombinations(array, 2);

          console.log(result.map(a => a.join(' ')));

          .as-console-wrapper max-height: 100% !important; top: 0; 





          function getCombinations(array, max) 

          function iter(i, j, temp)
          if (array[i] && j >= array[i].length)
          j = 0;
          i++;

          if (!array[i]

          var result = ;
          iter(0, 0, );
          return result;


          var array = [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']],
          result = getCombinations(array, 2);

          console.log(result.map(a => a.join(' ')));

          .as-console-wrapper max-height: 100% !important; top: 0; 






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 13:59

























          answered Nov 15 '18 at 13:46









          Nina ScholzNina Scholz

          192k15104176




          192k15104176























              3














              You could create recursive function using two nested for loops and one parameter to keep current row iteration count so you can start next for loop from there.






              const data = [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']]
              const res =

              function comb(data, n = 0, prev = )
              for (var i = n; i < data.length; i++)
              for (var j = 0; j < data[i].length; j++)
              let el = data[i][j]
              let arr = prev.concat(el);
              if (arr.length <= data[i].length) res.push(arr)
              comb(data, i + 1, arr)




              comb(data)
              console.log(JSON.stringify(res))








              share|improve this answer























              • nice idea with nested loops.

                – Nina Scholz
                Nov 15 '18 at 14:00











              • @Nina Scholz Thank you, not sure if it works correctly in other scenarios with different array lengths.

                – Nenad Vracar
                Nov 15 '18 at 14:01











              • i took a limit for max and exit the recursion.

                – Nina Scholz
                Nov 15 '18 at 14:02















              3














              You could create recursive function using two nested for loops and one parameter to keep current row iteration count so you can start next for loop from there.






              const data = [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']]
              const res =

              function comb(data, n = 0, prev = )
              for (var i = n; i < data.length; i++)
              for (var j = 0; j < data[i].length; j++)
              let el = data[i][j]
              let arr = prev.concat(el);
              if (arr.length <= data[i].length) res.push(arr)
              comb(data, i + 1, arr)




              comb(data)
              console.log(JSON.stringify(res))








              share|improve this answer























              • nice idea with nested loops.

                – Nina Scholz
                Nov 15 '18 at 14:00











              • @Nina Scholz Thank you, not sure if it works correctly in other scenarios with different array lengths.

                – Nenad Vracar
                Nov 15 '18 at 14:01











              • i took a limit for max and exit the recursion.

                – Nina Scholz
                Nov 15 '18 at 14:02













              3












              3








              3







              You could create recursive function using two nested for loops and one parameter to keep current row iteration count so you can start next for loop from there.






              const data = [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']]
              const res =

              function comb(data, n = 0, prev = )
              for (var i = n; i < data.length; i++)
              for (var j = 0; j < data[i].length; j++)
              let el = data[i][j]
              let arr = prev.concat(el);
              if (arr.length <= data[i].length) res.push(arr)
              comb(data, i + 1, arr)




              comb(data)
              console.log(JSON.stringify(res))








              share|improve this answer













              You could create recursive function using two nested for loops and one parameter to keep current row iteration count so you can start next for loop from there.






              const data = [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']]
              const res =

              function comb(data, n = 0, prev = )
              for (var i = n; i < data.length; i++)
              for (var j = 0; j < data[i].length; j++)
              let el = data[i][j]
              let arr = prev.concat(el);
              if (arr.length <= data[i].length) res.push(arr)
              comb(data, i + 1, arr)




              comb(data)
              console.log(JSON.stringify(res))








              const data = [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']]
              const res =

              function comb(data, n = 0, prev = )
              for (var i = n; i < data.length; i++)
              for (var j = 0; j < data[i].length; j++)
              let el = data[i][j]
              let arr = prev.concat(el);
              if (arr.length <= data[i].length) res.push(arr)
              comb(data, i + 1, arr)




              comb(data)
              console.log(JSON.stringify(res))





              const data = [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']]
              const res =

              function comb(data, n = 0, prev = )
              for (var i = n; i < data.length; i++)
              for (var j = 0; j < data[i].length; j++)
              let el = data[i][j]
              let arr = prev.concat(el);
              if (arr.length <= data[i].length) res.push(arr)
              comb(data, i + 1, arr)




              comb(data)
              console.log(JSON.stringify(res))






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 15 '18 at 13:46









              Nenad VracarNenad Vracar

              72.5k126083




              72.5k126083












              • nice idea with nested loops.

                – Nina Scholz
                Nov 15 '18 at 14:00











              • @Nina Scholz Thank you, not sure if it works correctly in other scenarios with different array lengths.

                – Nenad Vracar
                Nov 15 '18 at 14:01











              • i took a limit for max and exit the recursion.

                – Nina Scholz
                Nov 15 '18 at 14:02

















              • nice idea with nested loops.

                – Nina Scholz
                Nov 15 '18 at 14:00











              • @Nina Scholz Thank you, not sure if it works correctly in other scenarios with different array lengths.

                – Nenad Vracar
                Nov 15 '18 at 14:01











              • i took a limit for max and exit the recursion.

                – Nina Scholz
                Nov 15 '18 at 14:02
















              nice idea with nested loops.

              – Nina Scholz
              Nov 15 '18 at 14:00





              nice idea with nested loops.

              – Nina Scholz
              Nov 15 '18 at 14:00













              @Nina Scholz Thank you, not sure if it works correctly in other scenarios with different array lengths.

              – Nenad Vracar
              Nov 15 '18 at 14:01





              @Nina Scholz Thank you, not sure if it works correctly in other scenarios with different array lengths.

              – Nenad Vracar
              Nov 15 '18 at 14:01













              i took a limit for max and exit the recursion.

              – Nina Scholz
              Nov 15 '18 at 14:02





              i took a limit for max and exit the recursion.

              – Nina Scholz
              Nov 15 '18 at 14:02











              3














              If you need the order too (so single elements first, pairs last), that can be done too, just it is going to be a bit longer than the other solutions:






              var org=[['a1', 'a2'],['b1', 'b2'],['c1', 'c2']];

              var res=;

              // singles
              org.forEach(arr =>
              arr.forEach(elem =>
              res.push([elem]);
              );
              );

              // pairs
              var start=0;
              var end=res.length;
              org.forEach(arr =>
              start+=arr.length;
              arr.forEach(elem =>
              for(var i=start;i<end;i++)
              res.push([elem,res[i][0]]);
              );
              );

              console.log(JSON.stringify(res));





              //singles part is a nested pair of loops, pushing all the elements, and //pairs traverses the original array again, just it makes use of the already "flattened" result, end simply keeps the number of single elements (as the length of the array will grow) and start always jumps to the beginning of the next sub-array (so 'a1','a2'-like pairs are not generated, as it was required).






              share|improve this answer





























                3














                If you need the order too (so single elements first, pairs last), that can be done too, just it is going to be a bit longer than the other solutions:






                var org=[['a1', 'a2'],['b1', 'b2'],['c1', 'c2']];

                var res=;

                // singles
                org.forEach(arr =>
                arr.forEach(elem =>
                res.push([elem]);
                );
                );

                // pairs
                var start=0;
                var end=res.length;
                org.forEach(arr =>
                start+=arr.length;
                arr.forEach(elem =>
                for(var i=start;i<end;i++)
                res.push([elem,res[i][0]]);
                );
                );

                console.log(JSON.stringify(res));





                //singles part is a nested pair of loops, pushing all the elements, and //pairs traverses the original array again, just it makes use of the already "flattened" result, end simply keeps the number of single elements (as the length of the array will grow) and start always jumps to the beginning of the next sub-array (so 'a1','a2'-like pairs are not generated, as it was required).






                share|improve this answer



























                  3












                  3








                  3







                  If you need the order too (so single elements first, pairs last), that can be done too, just it is going to be a bit longer than the other solutions:






                  var org=[['a1', 'a2'],['b1', 'b2'],['c1', 'c2']];

                  var res=;

                  // singles
                  org.forEach(arr =>
                  arr.forEach(elem =>
                  res.push([elem]);
                  );
                  );

                  // pairs
                  var start=0;
                  var end=res.length;
                  org.forEach(arr =>
                  start+=arr.length;
                  arr.forEach(elem =>
                  for(var i=start;i<end;i++)
                  res.push([elem,res[i][0]]);
                  );
                  );

                  console.log(JSON.stringify(res));





                  //singles part is a nested pair of loops, pushing all the elements, and //pairs traverses the original array again, just it makes use of the already "flattened" result, end simply keeps the number of single elements (as the length of the array will grow) and start always jumps to the beginning of the next sub-array (so 'a1','a2'-like pairs are not generated, as it was required).






                  share|improve this answer















                  If you need the order too (so single elements first, pairs last), that can be done too, just it is going to be a bit longer than the other solutions:






                  var org=[['a1', 'a2'],['b1', 'b2'],['c1', 'c2']];

                  var res=;

                  // singles
                  org.forEach(arr =>
                  arr.forEach(elem =>
                  res.push([elem]);
                  );
                  );

                  // pairs
                  var start=0;
                  var end=res.length;
                  org.forEach(arr =>
                  start+=arr.length;
                  arr.forEach(elem =>
                  for(var i=start;i<end;i++)
                  res.push([elem,res[i][0]]);
                  );
                  );

                  console.log(JSON.stringify(res));





                  //singles part is a nested pair of loops, pushing all the elements, and //pairs traverses the original array again, just it makes use of the already "flattened" result, end simply keeps the number of single elements (as the length of the array will grow) and start always jumps to the beginning of the next sub-array (so 'a1','a2'-like pairs are not generated, as it was required).






                  var org=[['a1', 'a2'],['b1', 'b2'],['c1', 'c2']];

                  var res=;

                  // singles
                  org.forEach(arr =>
                  arr.forEach(elem =>
                  res.push([elem]);
                  );
                  );

                  // pairs
                  var start=0;
                  var end=res.length;
                  org.forEach(arr =>
                  start+=arr.length;
                  arr.forEach(elem =>
                  for(var i=start;i<end;i++)
                  res.push([elem,res[i][0]]);
                  );
                  );

                  console.log(JSON.stringify(res));





                  var org=[['a1', 'a2'],['b1', 'b2'],['c1', 'c2']];

                  var res=;

                  // singles
                  org.forEach(arr =>
                  arr.forEach(elem =>
                  res.push([elem]);
                  );
                  );

                  // pairs
                  var start=0;
                  var end=res.length;
                  org.forEach(arr =>
                  start+=arr.length;
                  arr.forEach(elem =>
                  for(var i=start;i<end;i++)
                  res.push([elem,res[i][0]]);
                  );
                  );

                  console.log(JSON.stringify(res));






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 15 '18 at 14:24

























                  answered Nov 15 '18 at 14:19









                  tevemadartevemadar

                  4,4832826




                  4,4832826



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319321%2fissue-with-extracting-combination-of-values-from-multiple-nested-arrays%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto