Javascript – Get Next and Previous Elements in JavaScript array

arraysjavascript

I have a large array, with non-sequential IDs, that looks something like this:

PhotoList[89725] = new Array();
PhotoList[89725]['ImageID'] = '89725';
PhotoList[89725]['ImageSize'] = '123';
PhotoList[89726] = new Array();
PhotoList[89726]['ImageID'] = '89726';
PhotoList[89726]['ImageSize'] = '234';
PhotoList[89727] = new Array();
PhotoList[89727]['ImageID'] = '89727';
PhotoList[89727]['ImageSize'] = '345';
Etc....

I'm trying to figure out, given an ID, how can I can get the next and previous ID… So that I could do something like this:

<div id="current">Showing You ID: 89726 Size: 234</div>
Get Prev Get Next

Obviously, if we're at the end or beginning of the array we just a message…

Best Solution

Why don't you add properties 'Prev' & 'Next' to that array?

PhotoList[89725] = new Array();
PhotoList[89725]['Prev'] = 89724;
PhotoList[89725]['Next'] = 89726;
PhotoList[89725]['ImageID'] = '89725';
PhotoList[89725]['ImageSize'] = '123';

This is just 'doubly-linked list' data structure.