var currentId = 1;
var list = [1,2,3,4,5,6,7,8,9]
var next = function(array,item){
var nextIndex = array.indexOf(item) + 1;
if(array.indexOf(array[nextIndex]) < 0)
return false;
return array[nextIndex];
}
var prev = function(array,item){
var prevIndex = array.indexOf(item) - 1;
if(array.indexOf(array[prevIndex]) < 0)
return false;
return array[nextIndex];
}
console.log(next(list,currentId),prev(list,currentId));
Enables a next and previous functionality for an array list
2 Responses
var prevIndex = array.indexOf(item) - 1;
if(array.indexOf(array[prevIndex]) < 0)
return false;
return array[nextIndex]; // nextIndex => prevIndex
}
Write a comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.