2

Alright, I know that there are a few posts about getting the product of an unknown amount arrays with unknown amount of elements -

JavaScript - Generating combinations from n arrays with m elements

Cartesian array based on array of objects in Javascript

To name a few - However, I have a need for a slight twist -

What I would like is a function that can output an Array of serialized objects instead of an Array of Arrays..

for example, if the input of the function were -

scenarioBuilder({ENV: ["QA","UAT"], HOST: ["APP1", "APP2", "APP3"], API: ["Example1", "Example2"]})

The output would be an array of serialized objects like this -

[{ENV: "QA", HOST: "APP1", API: "Example1"}, 
{ENV: "UAT", HOST: "APP1", API: "Example1"},
{ENV: "QA", HOST: "APP2", API: "Example1"},
{ENV: "UAT", HOST: "APP2", API: "Example1"},
{ENV: "QA", HOST: "APP3", API: "Example1"},
{ENV: "UAT", HOST: "APP3", API: "Example1"}]

etc. etc. etc.

Essentially taking the Array of ENV, HOSTS, AND API's - and making a serialized scenario builder.

I've tried a few cracks at adapting a few of the methods from the above links, but I have had no luck.

specifically -

function cartesian(...args) {
    var r = [], max = args.length-1;
    function helper(arr, i) {
        for (var j=0, l=args[i].length; j<l; j++) {
            var a = arr.slice(0); // clone arr
            a.push(args[i][j]);
            if (i==max)
                r.push(a);
            else
                helper(a, i+1);
        }
    }
    helper([], 0);
    return r;
}

But Javascript isn't my main language and I honestly have no clue how to break apart the Arrays and reassemble the objects.

Can anyone take a shot at it?

Thank you as always.

Nefariis
  • 3,451
  • 10
  • 34
  • 52
  • 1
    Your "attempt" doesn't seem anywhere close to what you want. Your function parameter is a single object. You can use `Object.entries()` to get an array of keys and values from that. – Barmar Dec 09 '21 at 00:23
  • And your output is supposed to be an array of objects, but nowhere in your code do you try to create any objects. – Barmar Dec 09 '21 at 00:24
  • The code is so far off that there's no way to correct it -- we would have to just write the whole thing from scratch for you. – Barmar Dec 09 '21 at 00:25
  • Maybe this is helpful: https://stackoverflow.com/questions/26697737/cartesian-product-of-javascript-object-properties – Barmar Dec 09 '21 at 00:26
  • This looks even closer: https://stackoverflow.com/questions/18957972/cartesian-product-of-objects-in-javascript – Barmar Dec 09 '21 at 00:27
  • I found those by googling "javascript cartesian product of object properties" – Barmar Dec 09 '21 at 00:27

1 Answers1

1

this a just a recursive matter...

const data = 
  { ENV : [ 'QA', 'UAT' ] 
  , HOST: [ 'APP1', 'APP2', 'APP3' ] 
  , API : [ 'Example1', 'Example2' ] 
  } 
 
const scenarioBuilder = objXarr =>
  {
  let names = Object.keys( objXarr )
    , len   = names.length -1
    , resp  = []
    ;
  buildIn( {}, 0)
  return resp

  function buildIn(obj, indx)
    {
    let key = names[indx]
    for (let val of objXarr[ key ] )
      {
      let oo = {...obj,[key]:val}
      if (indx < len )  buildIn(oo, indx +1)
      else resp.push( oo )
      }
    }
  }

console.log( scenarioBuilder( data ) )
.as-console-wrapper {max-height: 100%!important;top:0 }
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40