Get number of event with a specified name.

I look for a way to check how much of a specified event name I can find on a map.
For exemple I name many switch with the name Blue_Switch.
I want that when all switch are activated something happen.
Its can be done many other better way of course, but in this case, its for developpement speed, hard to explain...

in the same idea... a way to look of all of these same event have selfswith A activate will be great too.

I want some advice about how heavy that cann be thanks
Hi. Me again.
Code:
var ev = $dataMap.events;
for(var i = 1; i < ev.length; i++){
   if(ev[i].name === "EV005"){
      alert("EV005 ID is " + ev[i].id);
   }
}
Or from the event.
Code:
var name = $dataMap.events[this._eventId].name;
$gameMessage.add("Hi. My id is " + this._eventId + " and my name is " + name);
I'm paying it forward just like the web developers I asked for help on a daily basis 2007 to present at a web design forum until I could know javascript. I love helping people.

Those two codes will do these two things.
Screen%20Shot%202017-02-27%20at%2011.23.45%20PM.png


Screen%20Shot%202017-02-27%20at%2011.20.11%20PM.png
Hi. Me again.
Code:
var ev = $dataMap.events;
for(var i = 1; i < ev.length; i++){
   if(ev[i].name === "EV005"){
      alert("EV005 ID is " + ev[i].id);
   }
}
i put this code on event and also on map laod for see the result, that code seem do not work, its supose to do a message^
mogwai, you can eliminate the need for a for loop by using Array.prototype's filter function. Here's how you can do what the OP was asking for:

Code:
var events = $dataMap.events;
var matchingEvents = events.filter(function(ev) {
  if (ev) {
    return ev.name === "Blue_Switch";
  }
});
console.log("There are " + matchingEvents.length + " events called Blue_Switch.");
matchingEvents.forEach(function(ev) {
  $gameSelfSwitches.setValue([2, ev.id, 'A'], true);
});

This will iterate through each element of $dataMap.events, apply a function to it, then return a new array with the results. In this case, we're passing in the current element as "event", checking whether it's null (because $dataMap.events[0] is null and it'll cause an error if we don't check) then includes the event in the returned array if its name property matches "EV005". Then we run a forEach function on the returned array, also passing in a function, which sets the value of self switch A for each matching event's ID to true (2 would be whatever map ID you're doing this for).
for loop starts on 1 because 0 is null.
I'm aware of that. 🙂
mogwai, you can eliminate the need for a for loop by using Array.prototype's filter function. Here's how you can do what the OP was asking for:

Code:
var events = $dataMap.events;
var matchingEvents = events.filter(function(ev) {
  if (ev) {
    return ev.name === "Blue_Switch";
  }
});
console.log("There are " + matchingEvents.length + " events called Blue_Switch.");
matchingEvents.forEach(function(ev) {
  $gameSelfSwitches.setValue([2, ev.id, 'A'], true);
});

This will iterate through each element of $dataMap.events, apply a function to it, then return a new array with the results. In this case, we're passing in the current element as "event", checking whether it's null (because $dataMap.events[0] is null and it'll cause an error if we don't check) then includes the event in the returned array if its name property matches "EV005". Then we run a forEach function on the returned array, also passing in a function, which sets the value of self switch A for each matching event's ID to true (2 would be whatever map ID you're doing this for).
Can I optimize that:
Code:
var matchingEvents = $dataMap.events.filter(function(ev) {
  if (ev) {return ev.name === "Blue_Switch";}});
$gameVariables.setValue(101, matchingEvents.length);
var matchingEvents = $dataMap.events.filter(function(ev) {
  if (ev) {return ev.name === "Red_Switch";}});
$gameVariables.setValue(102, matchingEvents.length);
var matchingEvents = $dataMap.events.filter(function(ev) {
  if (ev) {return ev.name === "Yellow_Switch";}});
$gameVariables.setValue(103, matchingEvents.length);
var matchingEvents = $dataMap.events.filter(function(ev) {
  if (ev) {return ev.name === "Green_Switch";}});
$gameVariables.setValue(104, matchingEvents.length);
Sure, that would work.
Sure, that would work.
yes but a little bit ugly, anyway to be more beautifull^ with a select case maybe...
mogwai, you can eliminate the need for a for loop by using Array.prototype's filter function. Here's how you can do what the OP was asking for:

Code:
var events = $dataMap.events;
var matchingEvents = events.filter(function(ev) {
  if (ev) {
    return ev.name === "Blue_Switch";
  }
});
console.log("There are " + matchingEvents.length + " events called Blue_Switch.");
matchingEvents.forEach(function(ev) {
  $gameSelfSwitches.setValue([2, ev.id, 'A'], true);
});

This will iterate through each element of $dataMap.events, apply a function to it, then return a new array with the results. In this case, we're passing in the current element as "event", checking whether it's null (because $dataMap.events[0] is null and it'll cause an error if we don't check) then includes the event in the returned array if its name property matches "EV005". Then we run a forEach function on the returned array, also passing in a function, which sets the value of self switch A for each matching event's ID to true (2 would be whatever map ID you're doing this for).
I manage to do what i want with your code, thanks. I only need to find how to get map id now.
$gameMap._mapId
Moderator note: This thread is closed due to being solved.

Original thread: https://forums.rpgmakerweb.com/threads/get-number-of-event-with-a-specified-name.75401/