1

Suppose I have this array below:

['uploads']
    {
        ['file_1']
            {
                //maybe some attributes here
            }
        ['file_2']
            {
                //maybe some attributes here
            }
        ['folder_1']
            {
                ['file_1']
                    {
                        //maybe some attributes here
                    }
                ['file_2']
                    {
                        //maybe some attributes here
                    }
                ['folder_1']
                    {
                        ['file_1']
                            {
                                //maybe some attributes here
                            }
                        ['file_2']
                            {
                                //maybe some attributes here
                            }

                    }
            }
        ['folder_2']
            {
                ['file_1']
                    {
                        //maybe some attributes here
                    }
                ['file_2']
                    {
                        //maybe some attributes here
                    }
            }
    }

So let's say this array or object is called data. I would argue that ['uploads'] is level one; thus so far level one has only one entry. Level 2 of ['uploads'] has 4 entries, 2 files and 2 folders. If an entry is a folder it can have further levels, you get the picture.

All this is then created in jquery as clickable divs on a page. Only the first level of course.

So if the user clicks on folder_1, I want to populate another array called 'current_directory' with folder_1 of the 2nd level of ['uploads'] as follows: current_directory = data['uploads']['folder_1'];. This array is then created as clickable divs and again the user can click on something, and if it's a folder it will re-populate current_directory with the new folder, but this time as follows: current_directory = current_directory['folder_1']; Or whichever folder the user clicked on.

Now the problem comes when I want to go back to the previous directory. How do I maybe keep track of which level I'm in or something like check in data (which is the whole folder structure) in which key(folder) the array current_directory belongs.

Just thought you should know that no 2 keys, whether file or folder, in the same level can have the same name. And then also the files and folders will obviously not be called file_1 and folder_1 but user generated names.

Oh and then, what I mean by attributes is like more keys that only files can have like ['type'] and ['size'].

smnbbrv
  • 23,502
  • 9
  • 78
  • 109
  • It's just not an array and definitely not a piece of JavaScript... – smnbbrv May 17 '15 at 16:04
  • That is not code I literally typed in jquery to make the array, just a structure to show what it looks like. Should probably not have put it in as code, I understand that. The whole array is passed from a php script as a JSON object, so think of it as one. – BlindChicken May 17 '15 at 16:09

1 Answers1

0

The thing you are trying to do is usually made with stack data structure:

E.g. see simple implementation:

// create a stack named history
var history = []

// ...

// when a folder is expanded - add it to the stack
history.push(currentfolder)

// when going back - remove the last item
history.pop()
var current = history[history.length - 1]

So the idea is to have the track on all actions and simply "undo" the last one; the current folder is always accessible as the last item of your array.

To learn more see the article How do you implement a Stack and a Queue in JavaScript?

Community
  • 1
  • 1
smnbbrv
  • 23,502
  • 9
  • 78
  • 109
  • Works like a charm. It took me a while to figure out why my implementation did not work though, and then I changed the array name then it worked, maybe 'history' is reserved for something I don't know. – BlindChicken May 17 '15 at 22:18