-2
\$\begingroup\$

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?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ I suggest you re-read How do I ask a good question?. The title could make the question off-topic. What is this code really for? Remember we do general code reviews and do not answer specific questions. \$\endgroup\$
    – pacmaninbw
    Commented Apr 27, 2023 at 17:27

2 Answers 2

1
\$\begingroup\$

Much of your code is redundant because Javascript Arrays have an iterator already, use the iterator object instead of your own next, current, etc`. Here is what MDN has to say:

An object is an iterator when it knows how to access items from a collection one at a time, while keeping track of its current position within that sequence. In JavaScript an iterator is an object that provides a next() method which returns the next item in the sequence. This method returns an object with two properties: done and value.

Manually Hand-Rolling the iterator

Study iterators. Its underlying power and utility is not always obvious but stick with it, really easy to use once you know how.

\$\endgroup\$
2
  • \$\begingroup\$ Your link to Manually Hand-Rolling the Iterator was very helpful since I want to manually advance to the next item when a button is clicked. I'll try it out! \$\endgroup\$ Commented May 4, 2023 at 14:04
  • \$\begingroup\$ Update: the Iterator's .next() method was half of what I needed. I ended up using the code in my OP. It was just a POC to see what the UX felt like. It's working on my site here: public.aimfireready.com/flipper/single.html \$\endgroup\$ Commented May 4, 2023 at 16:14
0
\$\begingroup\$

This doesn't really match the definition of "cycle through them" - this is usually done with a (for) loop.

Anyway, if findIndex does not find any matching element, it returns -1. The prevIndex becomes -2 and there is no such element. NextIndex becomes 0 but it would be hard to argue that element zero is the next element after "no elememt".

Similarly if findIndex finds element with index 0. PrevIndex again becomes negative. And of course if findIndex returns the last element (at index data.length - 1) the nextIndex goes out of range of the data array size.

\$\endgroup\$
1
  • \$\begingroup\$ I don't want a for loop here. I want to load the first item in an HTML element, click the Next button, and have it load the next item in the array of objects. I will rephrase the question. \$\endgroup\$ Commented May 4, 2023 at 13:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.