1

let subcatregory = {
  "gender": ["male", "fame"],
  "education": ["matricution", "Inter"],
  "age": ["22", "23"]
}
let category = ['gender', 'education', 'age'];
var values = Dependciesvalues(subcatregory, category)

console.log('result', values)

function Dependciesvalues(Subcate, currentCombinations) {

  let firstCategory = '';
  let firstArray = []
  let temp = [];
  let dependciesvalues = []

  firstCategory = currentCombinations[0];

  for (let property in Subcate) {
    if (property === firstCategory) {
      firstArray = Subcate[property]
    } else {
      
      for (let makeDependencies in firstArray) {
        for (let inner in Subcate[property]) {

         
          
          dependciesvalues.push({
            'values': firstArray[makeDependencies] + ' ' + Subcate[property][inner]
          })
        }
      }
    }
  }
  return dependciesvalues;

}

I have object like this

subcatregory={"gender": ["male", "fame"],"education": ["matricution", "Inter"],"age": ["22", "23"]}
Category=['gender','education','age']

I want to make dependencies for like this

male -> matricution->22.
male -> matricution->23.
male -> inter->22.
male -> inter->23.
Female -> matricution->22.
Female -> matricution->23.
Female -> inter->22.
Female -> inter->23

this is my Code

var values= this.Dependciesvalues(subcategories, category)
  console.log(values);

 Dependciesvalues(Subcate,category){
  let firstCategory='';
  let firstArray=[]
  let temp=[];
  let dependciesvalues=[]
  
  firstCategory= category[0];

      for(let property in Subcate){
        if(property===firstCategory){            
          firstArray=Subcate[property]            
        }else{
          console.log('true',firstArray)
          for(let makeDependencies in firstArray ){
                for(let inner in Subcate[property] ){
                
                 temp=Subcate[property][inner]
                 console.log('valesss',  inner)
                  dependciesvalues.push({'values':firstArray[makeDependencies] + ' '+ Subcate[property][inner]})
                }
          }
        }
      }
       return dependciesvalues;

}

I am getting output like this which is wrong can you please help me out of this I really need to fix this soon

[ {values: "male matric"}
  {values: "male inter"}
  {values: "female matric"}
  {values: "female inter"} {values: "male 22"}
  {values: "male 23"}
  {values: "female 22"}
  {values: "female 23"}
 ]
Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42
  • Please provide a [mre], which should include all data and code needed to reproduce the output you are seeing. – Heretic Monkey Aug 10 '20 at 20:00
  • okay let me add – Ahmad Bilal Aug 10 '20 at 20:03
  • I add the code but its giving me error I don't know why – Ahmad Bilal Aug 10 '20 at 20:14
  • So, you've got `"fame"` in your snippet but `"Female"` in your desired output. Is that a typo, or do you want code that translates "fame" to "Female"? Computers are strict; they're going to return what you send in; precision in important when dealing with them. – Heretic Monkey Aug 10 '20 at 20:25
  • typo doesn't matter for now but really matters is I want my desired output ..can you please help me out of this ? – Ahmad Bilal Aug 10 '20 at 20:27
  • @HereticMonkey are you there? – Ahmad Bilal Aug 10 '20 at 20:36
  • I am, but I have a paying job. I volunteer my time to help people with their questions. I am not at your beck and call. There are many questions about getting the permutations of three arrays; with a little searching, I'm guessing you could find them. – Heretic Monkey Aug 10 '20 at 21:05
  • Does this answer your question? [JavaScript - Generating combinations from n arrays with m elements](https://stackoverflow.com/questions/15298912/javascript-generating-combinations-from-n-arrays-with-m-elements) – Heretic Monkey Aug 10 '20 at 21:07

1 Answers1

1

It looks like you're looking to get cartesian product by 3 categories, having 2 values each.

You can imagine possible combinations as a 3-dimensional box, comprised by cubic cells where each cell (combination) is uniquely addressed by respective categories values (coordinates).

Thus, in order to get all possible combinations you

  • multiply the number of cells (values) in each dimension (the size of the box, i.e. number of combinations) to prepare resulting array of appropriate size
  • scan your box cell by cell, populating resulting array with coordinates (combinations):

                               enter image description here

Following is a live-demo of that approach:

const src = {"gender": ["male", "female"],"education": ["matricution", "Inter"],"age": ["22", "23"]},

      
    cartesian = properties => {
      const periods = Object
              .keys(properties)
              .reduce((acc, key, i) => {
                acc[key] = acc.total
                acc.total *= properties[key].length
                return acc
              }, {total:1}),
            result = Array(periods.total)
              .fill()
              .map((_, i) => 
                Object.assign(
                  {},
                  ...Object
                    .keys(properties)
                    .map(key => 
                      ({[key]: properties[key][0|i/periods[key]%properties[key].length]}))
                )
             )
      return result
    }
    
console.log(cartesian(src))
.as-console-wrapper{min-height:100%;}
Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42