After making a request to an API, I received a JSON array with various attributes. One of the attributes is "State" and my task is to extract all the "EventName"(s) where State: "PR"
. The user will select the state from a dropdown menu.
In order to achieve this, I want to avoid using index numbers to access each attribute individually in the array. Instead, I am looking for a more efficient way to gather all the values that match the selected state (in this case, "PR").
So far, my attempt to filter out only the "EventName" values for "PR" resulted in a list of all the event names rather than just those specific to "PR". The desired outcome is to have a list like
pr_list = ["Debby 2000", "Dean 2001", "Jeane 2004" ... "Maria 2017"];
pr_list = [];
for (i = 0; i < event_data.length; i++) {
state_data = event_data[i].State;
if (state_data === "PR") {
console.log(event_data[i].EventName)
pr_list.append(event_data[i].EventName);
}
}