-2
\$\begingroup\$

I saw this piece of code while doing research for an idle game I am making:

var minions = new Object;
minions = {
    0: {
        name: "Mole",
        description: "...",
        price: 10,
        priceModifier: 1.5,
        attributes: {
            goldPerSec: 1
        }
    },
    1: {
        name: "Bulldog",
        description: "...",
        price: 1e3,
        priceModifier: 1.5,
        attributes: {
            goldPerSec: 70,
            diamondsPerSec: .5
        }
    },
    2: {
        name: "Giant Ant",
        description: "...",
        price: 1e5,
        priceModifier: 1.5,
        attributes: {
            goldPerSec: 700,
            diamondsPerSec: 3
        }
    },
    3: {
        name: "A.W.E.S.O.M.-O 2000",
        description: "...",
        price: 1e6,
        priceModifier: 1.3,
        attributes: {
            goldPerSec: 5e3,
            diamondsPerSec: 20
        }
    },
    4: {
        name: "Caterpillar",
        description: "...",
        price: 8e7,
        priceModifier: 1.3,
        attributes: {
            goldPerSec: 3e4,
            diamondsPerSec: 100
        }
    },
    5: {
        name: "Pterodactyl",
        description: "...",
        price: 19e8,
        priceModifier: 1.3,
        attributes: {
            goldPerSec: 85e3,
            diamondsPerSec: 400
        }
    },
    6: {
        name: "Miner Spaceship",
        description: "...",
        price: 29e9,
        priceModifier: 1.3,
        attributes: {
            goldPerSec: 5e5,
            diamondsPerSec: 2700
        }
    },
    7: {
        name: "The dwarf King",
        description: "...",
        price: 3e11,
        priceModifier: 1.3,
        attributes: {
            goldPerSec: 3e6,
            diamondsPerSec: 16e3
        }
    },
    8: {
        name: "Spirit Golem",
        description: "...",
        price: 5e13,
        priceModifier: 1.3,
        attributes: {
            goldPerSec: 25e6,
            diamondsPerSec: 98e3
        }
    }
};

And I have a small idea of what is being done here but my question is how would this code be handled or triggered.

Really all I need is the name for what this is, and I can take it from there :)

\$\endgroup\$
0

1 Answer 1

4
\$\begingroup\$

What you're looking at is an in-line initialization of an associative array (which functions similarly to a dictionary or map in other languages). If you made a variable in javascript like so:

var phones = {};

Then added values (right hand side) to keys (after the dot on the left)

phones.Steve = '2342311';
phones.Kyle = '3230009';

You will have an associative array (technically everything in javascript is an associative array but lets move on) with two keys. If you've never used them, read about javascript arrays. Continuing on to what you're seeing, my same 3 lines of code can be written like this:

var phones = { Steve: '2342311', Kyle: '3230009' };

Now, the keys value pairs are inside of brackets and separated by commas. When you see something like your example:

minions = {
0: {
    name: "Mole",
    ...
    attributes: {
        goldPerSec: 1
    }

0 is a key for minions with a value of another array (this concept is known in languages as a nested array or two dimentional arrays). attributes is an even more deeply nested array.

To use all of this, say you wanted that goldPerSec value, you'd retrieve it like this:

var gps = minions[0].attributes.goldPerSec;
\$\endgroup\$
4
  • 1
    \$\begingroup\$ This is exactly what I needed Thank you very much. \$\endgroup\$
    – Akmedrah
    Dec 24, 2015 at 5:16
  • \$\begingroup\$ @Akmedrah No problem. \$\endgroup\$
    – Foggzie
    Dec 24, 2015 at 5:17
  • 1
    \$\begingroup\$ Minor correction, you can't access an object attribute that's a number using the dot notation. min.0 won't work, while min[0] will. \$\endgroup\$
    – Patrick
    Dec 25, 2015 at 10:21
  • \$\begingroup\$ There are no associative arrays in JavaScript. There are Arrays which are numerically indexed, and there are Objects. I think you should rename Associative Array to Object in your answer to avoid confusion. \$\endgroup\$
    – bummzack
    Jan 5, 2016 at 19:02

Not the answer you're looking for? Browse other questions tagged .