[Javascript]How to get Actor Weapon's elemental damage ID?

Hello!
I'm making for my project a weakness/resistance system that doesn't use rates, but fixed values. For example, an enemy with resistance to Fire i'll make it that he removes 5 damage points from the total damage received from Fire attacks.
To do this, though, i need to be able to check the elemental damage inflicted by the weapon equipped by an actor. I tried looking for the right function to get the weapon's element ID, but i can't find it. Could you help me with this? Thanks in advance!
First of all, an actor can have more than one weapon, so I'll assume you're looking for the the elemental id of the first weapon. Second, I'll assume there's only one element per weapon, as my code will stop looking after the first traits that defines the weapon attack element (not to be confused with element rate in the first page of the traits). Here's the code which will return the element id, and if there's none it will return 0:
Code:
var weapon = $gameActors.actor(1).weapons()[0];
var element = 0;
for (var i = 0; i < weapon.traits.length; i++) {
    if (weapon.traits[i].code == 31) {
        element = weapon.traits[i].dataId;
        break;
    }
}
return element;

I've moved this thread to Learning Javascript. Thank you.

First of all, an actor can have more than one weapon, so I'll assume you're looking for the the elemental id of the first weapon. Second, I'll assume there's only one element per weapon, as my code will stop looking after the first traits that defines the weapon attack element (not to be confused with element rate in the first page of the traits). Here's the code which will return the element id, and if there's none it will return 0:
Code:
var weapon = $gameActors.actor(1).weapons()[0];
var element = 0;
for (var i = 0; i < weapon.traits.length; i++) {
    if (weapon.traits[i].code == 31) {
        element = weapon.traits[i].dataId;
        break;
    }
}
return element;
Moved to: Learning Javascript
I see, so the Element is stored in the traits.
Thank you so much!


EDIT: I've decided to work the code around to make it possible to get more than one element from a weapon. Here it is:


Code:
var a = $gameActors.actor(1);
var weaponElement = [];

if(a.weapons()[0] == null)
{
   weaponElement.push(1);
}
else
{
   for (var i = 0; i < a.weapons()[0].traits.length; i++)
   {
      if (a.weapons()[0].traits[i].code == 31)
      {
         weaponElement.push(a.weapons()[0].traits[i].dataId);
      }
   }
}

With the first If condition you avoid an error message when the actor has no weapon equipped

Original thread: https://forums.rpgmakerweb.com/threads/javascript-how-to-get-actor-weapons-elemental-damage-id.120843/