0

I have an array called gallery_list:

var gallery_list = ["profile.cfm", "explore.cfm", "list.cfm", "lesson.cfm", "class.cfm", "deploy_to_class.cfm"];

When I write the following, it works:

$.fancybox([
            { 'href' : gallery_list[0],
            'type' : 'iframe' },
            { 'href' : gallery_list[1],
            'type' : 'iframe' },
            { 'href' : gallery_list[2],
            'type' : 'iframe' },
            { 'href' : gallery_list[3],
            'type' : 'iframe' },
            { 'href' : gallery_list[4],
            'type' : 'iframe' },
            { 'href' : gallery_list[5],
            'type' : 'iframe' },

        ]);

But if I try to do something like below, it does not work:

var data = new Array();
        for (i = 0; i < gallery_list.length, i++) {
                var obj = {
                    'href' : gallery_list[i],
                    'type' : 'iframe'   
                }
                data.push(obj);
        }

$.fancybox([
            data
        ]); 

Can anyone provide any insight? Clearly I am getting something wrong with my data structures but I'm not sure what it is...

redconservatory
  • 21,438
  • 40
  • 120
  • 189
  • 2
    It's a good practice to create arrays as `var data = [];` instead of using `new Array()`. Check http://stackoverflow.com/a/1273936/275442 – julioolvr Jan 04 '12 at 17:39

3 Answers3

5

Your second example is an array inside an array. Instead:

$.fancybox(data);
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
4

Can you try something like

$.fancybox( data ); 
Adhip Gupta
  • 7,063
  • 7
  • 34
  • 46
0
$.fancybox([
            data
        ]);

Is redundant, data is already an array. [] is the operator for creating an array literal, so what you've done above is created an array, where the data array you've already constructed, is the first and only element.

As others have suggested what you want is the following:

$.fancybox(data);

To add a bit though, I think perhaps you would have avoided your mis-understanding had you initialized your "data" variable as follows:

var data = []; //instead of "new Array()"
for (i = 0; i < gallery_list.length, i++) {
    //etcetera
Dexygen
  • 12,287
  • 13
  • 80
  • 147