I am presented with an array of objects, each containing a property that is also an array:
const boards = [
{
id: 1,
name: 'Lorem ipsum',
tasks: [
{ id: 42, ... },
{ id: 65, ... },
{ id: 24, ... },
],
},
{
id: 2,
name: 'Lorem ipsum',
tasks: [
{ id: 12, ... },
{ id: 85, ... },
{ id: 14, ... },
],
},
];
I am interested in finding the most optimal and efficient method to locate the index of a specific task
(along with its corresponding board). While I have devised a solution, I am open to exploring alternative approaches that may offer improved performance, especially when handling larger datasets. As a note, the number of boards will generally be small (less than 10), but there could potentially be hundreds of tasks.
const idToFind = 1;
let boardIndex = null;
let taskIndex = null;
boards.some((board, index) => {
taskIndex = board.tasks.findIndex(task => task.id === idToFind);
// if the task is found, assign board index and terminate search
if (taskIndex > -1) {
boardIndex = index;
return true;
}
return false;
});