I am making a function that returns element of an array and return the prev and next element, but my problem is my function return the index of the elements of the array and i want to return the items
this is the array
var items = ['bill','hill','dill',4,5,6,7,8,9,10];
this is my function
function getItem(index, arr) {
if(index > arr.length) {
return -1;
}
const prev = index - 1;
const next = index + 1;
const prevPrev = prev - 1;
const nextNext = next + 1;
if(index == 0) {
return {index, next, nextNext}
} else if (index == arr.length - 1) {
return {prevPrev, prev, index}
} else {
return {prev, index, next};
}
}
output is
{prev: 1, index: 2, next: 3}
i want to return
{prev:'bill', index:'hill', next:'dill'}
getItemfunction{index: arr[index], next: arr[next], nextNext: arr[nextNext]}