I have a dataset and I want to search task name inside initialTasks array.
const stagesTasks = [
{
"dataTestID": "stage-0",
"headerText": "Backlog",
"initialTasks": ["task 1", "task 2", "task 3"]
},
{
"dataTestID": "stage-1",
"headerText": "To Do",
"initialTasks": ["task 4", "task 5", "task 6"]
},
{
"dataTestID": "stage-2",
"headerText": "Ongoing",
"initialTasks": ["task 7", "task 8"]
},
{
"dataTestID": "stage-3",
"headerText": "Done",
"initialTasks": ["task 9"]
}
]
For example if I want to know dataTestID for "task 8", I have to make two loops. Like below :-
getStageName = task => {
for(let record of stagesTasks){
for(let data of record.initialTasks){
if(data === task){
return record
}
}
}
return null
}
Just need your guidance to identify is there any way to avoid two loops?