MV I need help with filtering out values within an array.

I'm trying to create an array of all of the states that an actor is affected by within certain numbers listed in the database.
So for instance, let's say I'm trying to find all of the states that the actor is affect by within 1-400. And as an example let's say the actor is affected by states 12, 44, 189, and 333. How would I get these numbers into an array?

My ultimate goal is to have the plugin cycle through all of the states affecting the actor within the 1-400 range of states in the database and select one at random. Keep in mind the actor is affected by states beyond 400, so I don't want it to just cycle through every state currently applied to the actor.

JavaScript:
 abilities = [];
    for (var i = 1; i <= 400; i++) {
        abilities.push(i);
    }
        selectedability = abilities[Math.floor(Math.random()*abilities.length)];
        if ($gameParty.leader().isStateAffected(selectedability)) {
        selectedability += 400;
        $gameTemp.ability_checker[1] = 1;
        }

While the code above works to some degree, it unfortunately cycles through all of the 400 possible states, leaving long gaps in time where nothing is coming up; and I don't want this. The actor at most would only be affected by 10-15 of these specific states at a time so I know there's room for efficiency here.
$gameParty.leader().states().filter(state => state.id >= 1 && state.id <= 400)
$gameParty.leader().states().filter(state => state.id >= 1 && state.id <= 400)
Thanks for the quick reply, but how would I implement this line to get what I'm after? I'm having a bit of trouble with that.
That's what you'd set abilities to, and then you can loop through that array instead of doing a 400-iteration for loop. And you don't have to check to see if the actor has the state because you're only looping through states they have.
Thanks for the quick response again but unfortunately I think I'm having difficulty with the Math.random() part. Below is what I have but it's not giving me the results I'm looking for.


Code:
abilities = $gameParty.leader().states().filter(state => state.id >= 1 && state.id <= 400);
      
        selectedability = abilities[Math.floor(Math.random()*abilities.length)];
        selectedability += 400;
        $gameTemp.ability_checker[1] = 1;
The filter is giving you an array of state objects rather than numbers, and you can't add 400 to an object. 😉

JavaScript:
abilities = $gameParty.leader().states().filter(state => state.id >= 1 && state.id <= 400);
      
selectedability = abilities[Math.randomInt(abilities.length)].id;
selectedability += 400;
$gameTemp.ability_checker[1] = 1;
The filter is giving you an array of state objects rather than numbers, and you can't add 400 to an object. 😉

JavaScript:
abilities = $gameParty.leader().states().filter(state => state.id >= 1 && state.id <= 400);
     
selectedability = abilities[Math.randomInt(abilities.length)].id;
selectedability += 400;
$gameTemp.ability_checker[1] = 1;
Hmm, I applied this and the game gave me the error below as soon as the battle loaded(which this is a battle plugin btw).

1643572029735.png
That means $gameTemp.ability_checker is undefined. That's nothing to do with anything I've given you as it was part of your original code, so look at whatever was supposed to be defining that.
That means $gameTemp.ability_checker is undefined. That's nothing to do with anything I've given you as it was part of your original code, so look at whatever was supposed to be defining that.
Oh, lol it was a dumb oversight on my part. I changed the names of the values around to simplify my question and forgot to change them back for my project. Everything is working now and I really appreciate the help. You'll definitely be credited in my project for this.
Oh, lol it was a dumb oversight on my part. I changed the names of the values around to simplify my question and forgot to change them back for my project. Everything is working now and I really appreciate the help. You'll definitely be credited in my project for this.
I appreciate that, and you're more than welcome. Always happy to be of assistance. 🙂

Original thread: https://forums.rpgmakerweb.com/threads/i-need-help-with-filtering-out-values-within-an-array.144457/