[HELP]Return Enemy in Troop ?

Basically I need a code that checks what enemy is in what position inside the troop.

$gameTroop.members()[0] = enemyId(1)

I assume it's something like that but it doesn't work :/
The syntax is:
JavaScript:
$gameTroop.members()[index].enemyId()
Loop through the array until it returns the position of the member with the matching id.


JavaScript:
function getEnemyTroopPos(gameEnemyId) {
    var enemyTroopPos = 0;
    for (let i = 0; i < $gameTroop.members().length; i++) {
        if ($gameTroop.members()[i].enemyId() === gameEnemyId) {
            enemyTroopPos = i;
        };
    };
    return enemyTroopPos;
};
There's a quicker way to do it.

JavaScript:
function getEnemyTroopPos(gameEnemyId) {
  var enemies = $gameTroop.members().filter(member => member.enemyId() === gameEnemyid)
  return enemies.length > 0 ? enemies[0].index() : -1;
}
Yeah I don't have much understanding of the array methods yet except includes, find and findIndex and was indeed sitting there thinking of a less wordy way to write that before going back to the old school way lol.
Yeah I don't have much understanding of the array methods yet except includes, find and findIndex and was indeed sitting there thinking of a less wordy way to write that before going back to the old school way lol.
Actually I thought this was MV but in MZ you can streamline it yet further:

JavaScript:
function getEnemyTroopPos(gameEnemyId) {
  return $gameTroop.members().filter(member => member.enemyId() === gameEnemyId)[0]?.index();
}

Original thread: https://forums.rpgmakerweb.com/threads/help-return-enemy-in-troop.140329/