views:

1678

answers:

5

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...

+2  A: 

There's really no way other than to iterate through the possible ids sequentially until you find one which has an entry in your array. For example:

function findClosest(arr, id, increasing) {
    var step = increasing ? 1 : -1;
    for(var i=id+step; i>=0 && i<=max_id; i+=step)
        if( arr[id] )
            return id;
}

Obviously, this approach requires that you keep track of the max_id so that you don't iterate forever; here I assume that it's a global variable, but you might want to make it a parameter to the findClosest function. You'd call this function like so:

var prev = findClosest(arr, id, false);
var next = findClosest(arr, id, true);
Eli Courtwright
+4  A: 

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.

kuy
+5  A: 

Based on your example the IDs are sequential... This is another way of writing your example. new Array() really isn't what you should be using because those are objects you are creating. Also, I left the numbers as strings, but I'm not sure why you would want to do that. You could add next and prev like kuy suggested

PhotoList[89725] = {ImageID: '89725',
                    ImageSize: '123'};
PhotoList[89725] = {ImageID: '89726',
                    ImageSize: '234',
                    Next: '89727',
                    Prev: '89725'};
PhotoList[89725] = {ImageID: '89727',
                    ImageSize: '345'};

All of these are accessible just like your other structure.

Jerry
I'll second Jerry. Forget about the arrays. PhotoList should be an object as well. var PhotoList = { 89725: { ImageID: 89725, ImageSize: 123, next: 89726}, 89725: {} etc...
Prestaul
A: 
      var sibNum = 0;
      var sibList = [];
      var prevSiblingID = false;
      for (n in w) {
    sibNum++;
    sibList[n] ={
      title : n,
      prevSiblingID : prevSiblingID
    };
    if (prevSiblingID) {
      sibList[prevSiblingID].nextSiblingID = n;
    }
    prevSiblingID = n;
      };
      sibList[prevSiblingID].nextSiblingID = false;
Rene
A: 

No arrays at all are better..

images = {
    0: {
        size: 12345, /* dont realy need as you can use JS to mesure the size. */
        title: "day 1 on holiday"
    },
    1: {
        size: 13549, /* dont realy need as you can use JS to mesure the size. */
        title: "day 2 on holiday"
    },
    2: {
        size: 16548, /* dont realy need as you can use JS to mesure the size. */
        title: "day 3 on holiday"
    },
}

for(x in images){
    /* x = "the id of the image." */
    url[] = "/images/" + x + ".png";
    title[] = images[x].title;
    size[] = images[x].size;
    console.log("File: " + url[x] + " , Title: " + title[x] + " , Size: " + size + "bytes")
}
JamesM