redconservatory
redconservatory

Reputation: 21924

Javascript objects and syntax

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

Upvotes: -1

Views: 55

Answers (3)

Dexygen
Dexygen

Reputation: 12561

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

Upvotes: 0

Adhip Gupta
Adhip Gupta

Reputation: 7163

Can you try something like

$.fancybox( data ); 

Upvotes: 4

Dave Newton
Dave Newton

Reputation: 160181

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

$.fancybox(data);

Upvotes: 5

Related Questions