0

I'm currently creating a game where I want to store information about a Business, I do this through a multi-dimensional associative array so I have a list of the businesses and within the list key-value pairs of information.The array looks like this

Business.list =   {
        0.406411267183554: //First Business
            {
                capital : 5
                cash : 3 
                income: 3
                expenses : 6 
            },
        0.398198718278354:  //Second Business
            {
                capital : 6
                cash : 7 
                income: 3
                expenses : 7 
            }...

    };

Now I want to go one step deeper so that it now includes information about Upgrades. So within each Business, there will be information about all the upgrades that they have. These upgrades would be shown on the third level. I'm currently stumped on how I would programme this in Javascript. The way that I generate a business is by using Javascript Object Prototype.

    function Business(name, sector, capital, employees, type, id, efficiency) {
      var self = {
      name:name,  
      sector:sector,
      capital:capital,
      employees:employees,
      type:type,
      id:id,
      efficiency:efficiency,

     };

And then placing it into the list

    Business.list[id] = self;
3
  • 1
    and what does not work? Commented Feb 18, 2017 at 10:53
  • I'm not sure what you're asking, but self variable is not accessible outside of Business function. Also, there's global self, which refers to window. Commented Feb 18, 2017 at 10:57
  • If you visit this site deano.me/2012/05/… on the "Arrays – Associative" you can see that the array goes deeper and is 3 levels compared to only two for mine. How would I do this? Commented Feb 18, 2017 at 11:10

1 Answer 1

1
var self = {
  name:name,  
  sector:sector,
  capital:capital,
  employees:employees,
  type:type,
  id:id,
  efficiency:efficiency,
  upgrades: ['CreditCard'] 
 };

and then

upgrades = Business.list[id].upgrades 

or like this:

upgrades: {
    creditCard: false,
    cash: true 
} 

Is this what you want ?

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

4 Comments

Ohh on the right track! Instead of credit card being an array I was looking more for it to be an associative array so that "CreditCard = False"
then just change it to object or array of objects :)
Ok done! Thanks :) The code is quite messy as I'm doing it like this: upgrades: {'CreditCard' : false }... Would it be possible to create an object outside and insert it into the business object?
Yes it can. You can have it done separately. When starting you may not even have upgrades at all, just assign it later on e.g. Business.list[id].upgrades={'CreditCard': true}

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.