Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript enum with multiple values and value types

I'm trying to make a system which reads Dota 2 hero data, in this system I have to store a lot of data about each hero (strength gain, int gain, agility gain)(float) and also what their primary attribute is.

This is what I have so far:

const Heroes = {
Abaddon: 'Strength',
Alchemist: 'Strength',
AncientApparition: 'Intelligence',
AntiMage: 'Agility',
ArcWarden:'Agility',
Axe:'Strength',

}

tried this:

const Heroes = {
Abaddon: ('Strength','3.4', '2.4', '1.8', true),
Alchemist: ('Strength','2.8', '2.3', '1.6', true),
}
console.log(Heroes.Abaddon)

The output was just the last value (true)

like image 358
Infinite Avatar asked Sep 04 '25 16:09

Infinite


1 Answers

You could get creative and use Enums like Java uses them, for more than a way to rename integers like many other languages. Or you could simply use a standard JavaScript object like this:

const Heroes = {
  Abaddon: {
     primaryAttribute: 'Strength',
     attributeGains: {
        strength: 3.4,
        intelligence: 2.4,
        agility: 1.8
     }
  },
  Alchemist: {
     primaryAttribute: 'Strength',
     attributeGains: {
        strength: 2.8,
        intelligence: 2.3,
        agility: 1.6
     }
  }
};

Accessing the values are as simple as you would expect.

console.log(Heroes.Abaddon.primaryAttribute);
console.log(Heroes.Alchemist.attributeGains.agility);

I'm not sure why you are in need of an Enum specifically, but in the end, you will be making a complex but standard JavaScript object.

like image 100
ppovoski Avatar answered Sep 07 '25 07:09

ppovoski