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.

1

3 Answers 3

1

Did you try using includes? Like this:

let result = rosterData.find(x => x[dayOfWeek] && String(x[dayOfWeek]).includes(searchString));
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, first thing I tried. It returns the followng Uncaught TypeError: Cannot read property 'includes' of undefined
That means x[dayOfWeek] can be undefined (not set). Ive updated the answer to include a check.
Uncaught TypeError: x[dayOfWeek].includes is not a function
Perhaps its not a string sometimes? Added String() around it now
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!
0

just use like this

let result = rosterData.find(x =>(x[dayOfWeek]).includes(searchString));

Comments

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"
    }
}

Comments

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.