0

I am working with some exported roster information which I have converted to JSON. I have an array containing approximately 400 objects.

[
  {
    name: "Bloggs, Joe",
    position: "Employee",
    monday: "0700-1700 MC1",
    tuesday: "0600-1800 PX1",
    wednesday: "0600-1800 PX1",
    thursday: "RDO",
    friday: "RDO"
  },
  {
    name: "Doe, John",
    position: "Employee",
    monday: "0700-1700 Px1",
    tuesday: "0600-1800 MC1",
    wednesday: "0600-1800 MC!",
    thursday: "RDO",
    friday: "RDO"
  {,
  {
    name: "Smith, Aaron",
    position: "Employee",
    monday: "RDO",
    tuesday: "RDO",
    wednesday: "RDO",
    thursday: "0700-1700 MC1",
    friday: "0800-2000 PX2"
  }
]

I have written a JS function that takes the day of the week and the shift label as arguments and returns the name of the person working that shift. This works as intended, the problem is someone may be working the same duty but different hours which causes the function to return <Not Found>. I need to use find but match a substring and not the full string. Instead of searching for 0700-1700 MC1 just search for MC1, there will never be two people rostered on the same duty on the rostering software so conflicts shouldn't be an issue.

Here is my code that works with the full string

function rosterFind(dayOfWeek, searchString) {
    // Check Roster Data exists and is not null
    if (rosterData) {
        let result = rosterData.find(x => x[dayOfWeek] === searchString);
        
        return result ? result.name : "<Not Found>"
    } else {
        return "No Roster Data"
    }
}

How can I do rosterData.find() but with something like includes instead of the full string? I've tried several different approaches with little success. My JS is a bit rusty so it may be something obvious I am missing.

0xdw
  • 3,755
  • 2
  • 25
  • 40
iShaymus
  • 512
  • 7
  • 26
  • 1
    Does this answer your question? [How to check whether a string contains a substring in JavaScript?](https://stackoverflow.com/questions/1789945/how-to-check-whether-a-string-contains-a-substring-in-javascript) – yqlim Nov 07 '20 at 02:35

3 Answers3

1

Did you try using includes? Like this:

let result = rosterData.find(x => x[dayOfWeek] && String(x[dayOfWeek]).includes(searchString));
Emre Koc
  • 1,481
  • 10
  • 18
  • Yes, first thing I tried. It returns the followng `Uncaught TypeError: Cannot read property 'includes' of undefined` – iShaymus Nov 07 '20 at 02:36
  • That means `x[dayOfWeek]` can be undefined (not set). Ive updated the answer to include a check. – Emre Koc Nov 07 '20 at 02:37
  • `Uncaught TypeError: x[dayOfWeek].includes is not a function` – iShaymus Nov 07 '20 at 02:39
  • Perhaps its not a string sometimes? Added `String()` around it now – Emre Koc Nov 07 '20 at 02:40
  • Spot on. I think the parsing was on some rows picking up dates from excel which it was coverting to json as integers. Casting to `String` made it work. Thanks heaps! – iShaymus Nov 07 '20 at 02:46
0

just use like this

let result = rosterData.find(x =>(x[dayOfWeek]).includes(searchString));
Balaji
  • 9,657
  • 5
  • 47
  • 47
0

Just posting another approach with Regular Expressions:

function rosterFind(dayOfWeek, searchString) {
    // Check Roster Data exists and is not null
    if (rosterData) {
        const re = new RegExp(searchString);
        let result = rosterData.find(x => re.test(x[dayOfWeek])); // returns true on the first match
        
        return result ? result.name : "<Not Found>"
    } else {
        return "No Roster Data"
    }
}
jayg_code
  • 571
  • 8
  • 21