2

I have made a request to an API that is returning a JSON array like this:

enter image description here

My end goal is to take the attribute "State" and find all the "EventName" (s) where State: "PR" (or another state. I am getting the State input from a user dropdown) and put that into a list.

I know I can use the index (Ex: event_data[0].State returns "PR") to get the individual attributes but how can I avoid using the index to get all the State value (or EventName values) in the entire Array? Or is that even wise? I have tried the below, but it seemed to just grab all of the EventName values rather than just those for "PR". The expected output should be a list of event names for just "PR" 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);
  }
}
2
  • 2
    Please share expected output Commented Aug 20, 2019 at 13:54
  • you're missing == in the if statement: if (state_data = "PR") - you're performing assignment, not comparison, thats why it takes all eventName and not just those with State equal to "PR" Commented Aug 20, 2019 at 13:56

4 Answers 4

2

You could use Array.filter() and Array.map() for this:

filtered_events = event_data.filter(event => (event.State === "PR"));
pr_list = filtered_events.map(event => event.EventName);

Regarding your existing code - you have a typo:

if (state_data = "PR") { ... }

should be:

if (state_data === "PR") { ... }

(more info on MDN)

Sign up to request clarification or add additional context in comments.

3 Comments

This works great up front but I just want the EventName values in a list for "PR". pr_list = event_data.filter(event.EventName => (event.State === "PR")) doesn't quite work though.
@gwydion93 I noticed that after I posted my answer - it should do what you want now
That works perfect! It was the filter that was the key.
2

You could use Array.filter() and Array.map() for this:

pr_list = event_data.filter(event => (event.State === "PR")).map(event => event.EventName)

for unicity use a Set :

pr_list = [...new Set(event_data.filter(event => (event.State === "PR")).map(event => event.EventName))]

Comments

2

Native Way

const finalArray = [];
const event_data = [{ EventName: 'Event1', State: 'PR' }, { EventName: 'Event2', State: 'PQ' }];
for (i = 0; i < event_data.length; i += 1) {
  element = event_data[i];
  if (element.State === "PR") {
    finalArray.push(element.EventName);
  }
}
console.log(finalArray);

ES6 Way

const event_data = [{ EventName: 'Event1', State: 'PR' }, { EventName: 'Event2', State: 'PQ' }];
const finalArray = event_data.filter((f) => {
  return f.State &&
    f.State === 'PR';
}).map((m) => { return m.EventName; });
console.log(finalArray);

Comments

2

You can using array.filter() function to get list have state is PR and array.map() function to get all eventId of list.

var array = [
  {eventId: '1', State: 'PR'},
  {eventId: '2', State: 'AR'},
  {eventId: '3', State: 'PR'}
];

// get all event with State is PR
array = array.filter(function (event) {
  return event.State === "PR";
});

// put all eventId in new array
var eventIds = array.map(function (event) {
   return event.eventId
});

console.log('eventIds:', eventIds);

2 Comments

I like this solution. I would use eventName instead but it may be useful to have a new array rather than just a list.
Ok, this is demo code so it is maybe not perfect. I'm happy to know that you like this solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.