0

I have a Java script array as below: - the first element is a string - the second element is a array -- the first element of this is a string -- the second element is an array --- the first element of this is a string --- the second element is an array : : Practically, this goes on to a level of 5.

photoList = 
    [Photographs, [
                    [2000, [
                             [London, [pic1.jpg, pic2.jpg, pic3.jpg]],
                             [Rome,   [p1.jpg, p2.jpg, p3.jpg....]]
                           ]],
                    [2001, [
                             [Berlin, [x1.jpg, x2.jpg,....]],
                             [Munich, [y1.jpg, y2.jpg, y3.jpg,...]],
                             [Frankfurt, [z1.jpg, z2.jpg]]
                           ]]
                  ]
    ];

I know the path to a selected photograph (and the index values are stored in an array "path") Example [1][0][1][2] is Year 2000 -> Rome -> p3.jpg

Knowing these index values, how can I access p3? In the code, I formed a string variable: var index="[1][0][1][2]". Now I need to do something like: photoList+index I tried eval(photoList + index), which does not work. I also tried using a loop, with slice:

var elem = photoList;
for (var i=0; i<path.length; i++) {
  elem = elem.slice(path[i], path[i+1];
}

Any ideas would be appreciated.

user3236820
  • 83
  • 1
  • 8
  • possible duplicate of [Accessing nested JavaScript objects with string key](http://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-with-string-key) – Felix Kling Mar 08 '14 at 01:22
  • `console.log(photoList[1][0][1][2])`. Now `console.log(photoList[1][0][1][1][1][2])`. – StackSlave Mar 08 '14 at 01:30
  • This has many unbalanced square brackets: `[` 16 times and `]` only 10 times. So it's not clear exactly what you really meant. So the rest of this comment may be inaccurate. Shouldn't `[1][0][1][2]` be `[1][0][1][1][1][2]`? Where did you get this data structure? You should use objects, not just arrays. Something like this: `photoList = {Photographs: {'2000': {London: ['pic1.jpg', 'pic2.jpg', 'pic3.jpg'], Rome: ['p1.jpg', 'p2.jpg', 'p3.jpg', ...]}, '2001': {Berlin: [...], Munich: [...], Frankfurt: [...]}}};` Then the array with Drasko Kokic's answer would be `['Photographs','2000','Rome',2]`. – David Knipe Mar 08 '14 at 02:30
  • I had to replace my real data with what I typed above, and may have introduced the mismatched brackets. But I have tried to correct it, However I hope you got the idea of the data structure, a hierarchical list of photographs, arranged by city within year. – user3236820 Mar 15 '14 at 09:18

2 Answers2

0

Why not doing the following (provided the path is an array of indexes, e.g [1, 0, 1, 2]):

var elem = photoList;
for (var i=0; i<path.length; i++) {
  elem = elem[path[i]];
}
Draško Kokić
  • 1,280
  • 1
  • 19
  • 34
  • Yes, that works now ! I tried that before, but had something else messing it up. Thank you! – user3236820 Mar 08 '14 at 01:26
  • I know what you mean. Very often it is just some small detail which cause the problem. This is where another pair of eyes could help ;-) I would very much appreciate if you could accept the answer. – Draško Kokić Mar 08 '14 at 01:42
  • I cannot increase any scores until I rack up 15 reputation. But I will come back when hopefully I am at that level. – user3236820 Mar 16 '14 at 09:25
  • Oh okay. But you surely can accept an answer to your question, right? – Draško Kokić Mar 16 '14 at 23:55
  • I thought to accept an answer was to increase the score. Is there something else you want me to do? Write "I accept this answer"? Please tell me. – user3236820 Mar 18 '14 at 02:36
  • There should be a check icon left to each answer. By clicking on it you accept that answer. Regards ;-) – Draško Kokić Mar 21 '14 at 21:25
0

In order to eval photoList+index you would need it to pass photoList as a string to eval, so eval("photoList" + index) would work.

DanArl
  • 1,113
  • 8
  • 10
  • Yes, it does!! I like this better than the loop solution below. Although I still loop to form the index string, I'm not looping to extract array elenents. – user3236820 Mar 08 '14 at 01:40
  • Using `eval` (or "evil" as I like to call it) is widely discouraged. Please find a better way. – David Knipe Mar 08 '14 at 02:07
  • Agreed - eval is not the ideal way. – DanArl Mar 13 '14 at 01:42
  • I am not sure what is evil about this solution. Neither one of the values within the eval paren is obtained from the UI, so no user injection of malicious code. Perhaps if it is so evil, it should be removed from Javascript completely... Ah! but there's good reason to keep it too you say, but the above type of solution is not one of them? – user3236820 Mar 15 '14 at 09:30