0

I'm trying to write a function to give permutations given an array of data like this:

  [ 
    { ent: 'animal', vals: [ 'dog', 'cat' ] },
    { ent: 'color', vals: [ 'red', 'blue', 'green' ] },
    { ent: 'owner', vals: [ 'bob', 'david' ] } 
  ]

I'd like a result like:

  [
    [animal: dog, color; red, owner: bob],
    [animal: dog, color: red, owner: david],

    [animal: dog, color: blue, owner: bob],

    // ... etc (values as strings)
  ]

or even just

[ dog, red, bob ],
[ dog, red, david ],
[ dog, blue, bob ],
// etc (as strings)

basically the Unique set like:

111
112
113
121
122
123
// etc

Unique combinations that have a value for each of the options.

I've been struggling with some type of recursive function to to this without too much luck!

related for simple strings Permutations in JavaScript?

I've found some libraries for dealing with Permutations but nothing that seems to work with this type of structures.

Thanks!

dcsan
  • 11,333
  • 15
  • 77
  • 118
  • do a Depth for Search function – JSmith Aug 25 '18 at 23:21
  • Is this just for these three arrays, or for any number of arrays? If it's just three, you can use nested loops. Also, this looks like combinations (order doesn't matter) than permutations (order matters). – ggorlen Aug 25 '18 at 23:23
  • it's an arbitrary number of properties – dcsan Aug 25 '18 at 23:25
  • 1
    Do you want *permutations* or *combinations*. In other words do you want `[dog, red, bob]` && `[red, dog, bob]` in the results? – Mark Aug 25 '18 at 23:25
  • I just want the unique combinations. will clarify question – dcsan Aug 25 '18 at 23:29
  • @ggorlen if just using nested loops for say 3 deep, how would you write that? – dcsan Aug 25 '18 at 23:32
  • 2
    Possible duplicate of [Finding All Combinations of JavaScript array values](https://stackoverflow.com/questions/4331092/finding-all-combinations-of-javascript-array-values) – S.V. Aug 25 '18 at 23:33

2 Answers2

0

Just a note, what you are trying to do isn't finding permutations. You are trying to find all combinations of items from different arrays (1 from each array).

Here is 'pseudocode' to generate the combinations:

var data=[ 
    { ent: 'animal', vals: [ 'dog', 'cat' ] },
    { ent: 'color', vals: [ 'red', 'blue', 'green' ] },
    { ent: 'owner', vals: [ 'bob', 'david' ] } 
]

var results=[]
function generateCombinations(index, currentResult)
    if index == data.length //base case
        results.push(a copy of currentResult)

    var ent=data[index].ent
    for each value in data[index].vals //loop through all possible values of current data
        currentResult[ent]=value
        generateCombinations(index+1, currentResult)

generateCombinations(0,{})
console.log(results)

That should show on the console what you want. Note to create a copy of an object use Object.assign.

The recursive function is a bit difficult to explain. It is probably easier to take a look at the function yourself and figure out how it works.

TLDR: use a DFS (depth first search) to generate all combinations

0

This is similar to a normal combination function but a little easier since you can go through the levels on at a time.

Here's a simple recursive approach:

let arr = [ 
    { ent: 'animal', vals: [ 'dog', 'cat' ] },
    { ent: 'color', vals: [ 'red', 'blue', 'green' ] },
    { ent: 'owner', vals: [ 'bob', 'david' ] } 
  ]

function getCombinations(arr){
    if (arr.length === 0) return [[]]
    let [current, ...rest] = arr
    let combinations = getCombinations(rest)
    return current.vals.reduce((a, string) => 
        [ ...a, ...combinations.map(c => [string, ...c])], [])
}
let c = getCombinations(arr)
console.log(c)
Mark
  • 90,562
  • 7
  • 108
  • 148
  • It would be helpful to have some sort of explanation. Also, if this is simple, I'd hate to see complex. – skube Jul 04 '19 at 20:22