vendredi 23 octobre 2020

Check if string matches

I've got a dataset, and each object has a promoUrl and a promoNumber, structured like so:

const phoneNumbers = [
  {
    promoUrl: '/interior-doors/',
    promoNumber: '589-918-0710',
  },
  {
    promoUrl: '/promo4/',
    promoNumber: '307-789-8615',
  },
];

On first load a cookie is set, containing the referral url (the const referrer in the code below) and the referral URL is passed to a reduce function allowing me to look for a URL, find the associated phone number, and then display that number dynamically.

const url = referrer;

const promoNumber = promoResults.reduce((promoNumber: string, results: any) => {
  const hasPromo = url === results.promoUrl;

  if (hasPromo) {
    return results.promoNumber;
  }
    return promoNumber;
}, '');

However right now when it finds the URL it will only match the number if the cookie value matches the promoUrl exactly. Once this is live I won't be in charge of setting the promoUrls, and that task will go to non-developers. How do I set this so it works as long as the string contains matching characters, i.e. instead of needing /interior-doors/ it would work if the promoUrl is interior-doors or /interior-doors? Really anything so long as the string includes matching characters?

I've tried editing my hasPromo const using .includes():

const hasPromo = url.includes(results.promoUrl);

But it still hasn't worked for me.

Aucun commentaire:

Enregistrer un commentaire