1

I have these arrays

countryList = ["United Kingdom", "France", "Tajikstan"]

countryDetails = ["Markets", "Government", "Details"] 

countryFactors = ["Capital", "Labour", "Land"]

marketList = ["Primary", "Secondary", "Service"]

businessList = ["Businesses", "Info"]

supplyAndDemand = ["Supply", "Demand", "Price"]

I want these arrays to form an object as follows

Country.list ={
"United Kingdom":{
    "Markets":{ 
        "Primary":{
            "Businesses":{},
            "Info":{
                "Suppy":0,
                "Demand":0,
                "Price":0,
            },
        },
        "Secondary":{
            "Businesses":{},
            "Info":{
                "Suppy":0,
                "Demand":0,
                "Price":0,
            },
        },
        "Service":{
            "Businesses":{},
            "Info":{
                "Suppy":0,
                "Demand":0,
                "Price":0,
            },
        },
    },
    "Government":{

    },
    "Details":{
        "Capital":{
            "Info":{
                "Suppy":0,
                "Demand":0,
                "Price":0,
            },
        },
        "Labour":{
            "Info":{
                "Suppy":0,
                "Demand":0,
                "Price":0,
            },
        },
        "Land":{
            "Info":{
                "Suppy":0,
                "Demand":0,
                "Price":0,
            },
        },
    },  
},

"France":{
    "Markets":{ 
        "Primary":{
            "Businesses":{},
            "Info":{
                "Suppy":0,
                "Demand":0,
                "Price":0,
            },
        },
        "Secondary":{
            "Businesses":{},
            "Info":{
                "Suppy":0,
                "Demand":0,
                "Price":0,
            },
        },
        "Service":{
            "Businesses":{},
            "Info":{
                "Suppy":0,
                "Demand":0,
                "Price":0,
            },
        },
    },
    "Government":{

    },
    "Details":{
        "Capital":{
            "Info":{
                "Suppy":0,
                "Demand":0,
                "Price":0,
            },
        },
        "Labour":{
            "Info":{
                "Suppy":0,
                "Demand":0,
                "Price":0,
            },
        },
        "Land":{
            "Info":{
                "Suppy":0,
                "Demand":0,
                "Price":0,
            },
        },
    },  
  },
}

So basically depending on the name of the array, dictates what will follow after. So if the array name is "Markets" then underneath that array will be "marketList" array. etc

Does anyone have an idea? I was thinking of doing an if statement after the loop but the if statement has been modifying my code.

Here's an example of my code of what I've been doing: https://jsfiddle.net/4vcru3a0/

7
  • Regex's and case statements, inside nested forEachs, maybe... Commented Mar 20, 2017 at 17:14
  • either your question is not clear, or you are trying to automate a business logic which is not possible(which YOU have to program) Commented Mar 20, 2017 at 17:14
  • singsuyash, not sure why would you term this impossible. Reasons? Seems like a good question to me. Commented Mar 20, 2017 at 17:16
  • Contrary to your description, there's no obvious link between array entries and the lower-level array name. Markets -> marketList, Details -> countryFactors... There's no way to automate this as is, it's just going to be a number of nested loops. Commented Mar 20, 2017 at 17:16
  • Your code is working fine after a minor issue fixed. You just need to handle the case of Businesses, and Info following the same procedure. I see right output in console jsfiddle.net/zphwak0h . Am I missing something? Commented Mar 20, 2017 at 17:21

1 Answer 1

2

You could use a dynamic approach for any depth of nesting objects.

function iter(object, i) {
    data[i].forEach(function (k) {
        if (i + 1 < data.length) {
            object[k] = {};
            iter(object[k], i + 1);
        } else {
            object[k] = 0;
        }
    });
}

var data = [["United Kingdom", "France", "Tajikstan"], ["Markets", "Government", "Details"], ["Capital", "Labour", "Land"], ["Primary", "Secondary", "Service"], ["Businesses", "Info"], ["Supply", "Demand", "Price"]],
    tree = {};

iter(tree, 0);
console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

2 Comments

This is fine if the structure is consistent but there are abnormalities in the structure which I'm looking to create (you can see the structure I'm looking for in the example I included in my question).
Whoops I fixed the problem, in the if statement it should've been a double "==", I'm an idiot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.