Using plain JavaScript, I want to load an array of objects, display the first one in a Bootstrap Card, and then click a button to advance to the next one. Focusing only on the point of getting the next object in the array, is this a good/bad/ugly/decent/standard technique?
const data = [
{
"id": "168ad7d0",
"project_name": "Engineering",
"task_name": "Realign the warp core.",
"task_priority": 1,
"task_due_date": "2023-06-26"
},
{
"id": "d0112cbc",
"project_name": "Operation Destroy Earth",
"task_name": "Fire the space modulator",
"task_priority": 9,
"task_due_date": "2023-06-15"
},
{
"id": "01ca15a3",
"project_name": "Take Over the World",
"task_name": "Same thing we do every night, Pinky",
"task_priority": 2,
"task_due_date": ""
},
{
"id": "77fd2d1d",
"project_name": "Perimeter Cameras",
"task_name": "Install exterior cameras",
"task_priority": 1,
"task_due_date": "2023-06-15"
}
]
let currentId = 'd0112cbc' //TEST
let currentIndex = data.findIndex(x => x.id === currentId);
currentItem = data[currentIndex]
prevIndex = (currentIndex-1)
prevItem = data[prevIndex]
nextIndex = (currentIndex+1)
nextItem = data[nextIndex]
// Log to console
console.info(`Searching for id: '${currentId}'...`)
console.info('')
console.info(`Previous Item is #${(prevIndex+1)}: ${prevItem.task_name}`)
console.info(`Current Item is #${(currentIndex+1)}: ${currentItem.task_name}`)
console.info("Next Item is #" + (nextIndex+1) + ": " + nextItem.task_name)
Bonus question: is this called an algorithm?