/    Sign up×
Community /Pin to ProfileBookmark

Problem with Arrays

Hi everyone ?

The follow array:

[CODE]
var data = {
‘1.jpg’: { caption: ‘Picture1.’ },
‘2.jpg’: { caption: ‘Picture2.’ },
‘3.jpg’: { caption: ‘Picture3.’ }
};
[/CODE]

But i’d like to create that var dynamically.

Like that…

[CODE]

var numberElements = 3;
var i=0;
var data = new Array();
for(i=0; i<numberElements; i++)
data[i] = ‘1.jpg’: { caption: ‘Picture1.’ };

[/CODE]

But this doesn’t work…

Any help?!?!

Thks

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Jul 24.2009 — That is not an Array that is an Object.

[CODE]var data = {};
for(var i=1; i<=numberElements; i++){
data[i + ".jpg"] = { caption: 'Picture' + i + '.' };
}[/CODE]


Eric
Copy linkTweet thisAlerts:
@luis86authorJul 24.2009 — Thank you very much. you're amazing! ?


Problem Solved!?
×

Success!

Help @luis86 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...