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.