@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
21 lines (18 loc) • 629 B
JavaScript
import findIndex from 'lodash/findIndex'
import isString from 'lodash/isString'
export function getNextItemIdFromArray(itemId, items) {
if (items.length > 1) {
// the array can have objects or strings
const indexCurrentItem = findIndex(items, item => item.id === itemId || item === itemId)
// get previous item if exists, else the next item
const nextItem = items[indexCurrentItem - 1] || items[indexCurrentItem + 1]
if (nextItem.id) {
// if nextItem is an object with an id
return nextItem.id
}
if (isString(nextItem)) {
// if nextItem a sting
return nextItem
}
}
}