0

i'm trying to match the value of variable to an object array. the last if statement is what i'm struggling with. anything helps thanks in advance.

function getPage(){
    let pageActual = window.location.href;
    console.log(pageActual);

    let pageName = /500(.*)/;
    let EventName = pageName.exec(pageActual); // this creates the event name based on the event url you're currently on.
    console.log(EventName[1]);
const events2020v2 = [

{event:"example1", Date:'February 19, 2020'},
{event:"test2.html", Date:'February 11, 2020'},
{event:"example3", Date:'February 19, 2020'},
{event:"/test2.html", Date:'February 19, 2020'},
{event:"example4", Date:'February 19, 2020'}




];
// below im attempting to match the eventName to a value in the object above.
if(events2020v2.event.indexOf(EventName.value)){
         console.log('something')
     }
Eddiejm007
  • 11
  • 2

1 Answers1

0

So .indexOf() works a bit differently. I suggest for this scenario .find() instead.

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

The find() method returns the value of the first element in the provided array that satisfies the provided testing function.

Take a look at my example for your case:

const EventName = {
  value: 'test2.html'
};

const events2020v2 = [
   {event:"example1", Date:'February 19, 2020'},
   {event:"test2.html", Date:'February 11, 2020'},
   {event:"example3", Date:'February 19, 2020'},
   {event:"/test2.html", Date:'February 19, 2020'},
   {event:"example4", Date:'February 19, 2020'}
];

const found = events2020v2.find(e => e.event === EventName.value);

if (found) {
  console.log('found', found);
}

I hope that helps!

Community
  • 1
  • 1
norbitrial
  • 14,716
  • 7
  • 32
  • 59