/    Sign up×
Community /Pin to ProfileBookmark

Object creation and population

Hi! I have got this code:
var data;
data = {items: [
{value: “Value1”},
{value: “Value2”},
{value: “Value3”},
]};

I would want to declare data and to add Values dinamically, because sometimes i have got 2 values, other times 3,…
How could i do this?

Thank you very much!!!

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@MikeOSNov 14.2010 — [CODE]data = {items: new Array()};[/CODE]

Now add items when you need to using the push() method like so:

[CODE]data.items.push('item 1');
data.items.push('item 2');
data.items.push('item 3');[/CODE]
Copy linkTweet thisAlerts:
@mahoniauthorNov 14.2010 — Thanks for your reply.

It doesnt work for me.

In my example:

data = {items: [

{value: "Value1"},

{value: "Value2"},

{value: "Value3"},

]};

[B]if i put this alert:

alert(data.items[0].value);

The result is: Value1[/B]


But in your example if i put the same alert, the result is undefined:

var data = {items: new Array()};

data.items.push("Value1");

data.items.push("Value2");

data.items.push("Value3");

[B]alert(data.items[0].value);[/B]

Result: undefined

Why?

Thanks!!!!!
Copy linkTweet thisAlerts:
@MikeOSNov 14.2010 — Remove the value property from my example code, just have:

alert(data.items[0]);
Copy linkTweet thisAlerts:
@mahoniauthorNov 14.2010 — My problem is the next one:

I need to be exactly like this:

data = {items: [

{value: "Value1"},

{value: "Value2"},

{value: "Value3"},

]};

But dynamically generated. Why?

Because i am using jquery autosuggest plugin(https://github.com/fredwu/jquery-autosuggest) and if you pass this code, it recognize Value1, Value2,Value3.

But if i pass your code it does not recognize any values.

I think that "value" is important, but no idea why.

Thanks, thanks, thanks!!!
Copy linkTweet thisAlerts:
@MikeOSNov 14.2010 — Pass on object in the correct format to the push() method instead:

[CODE]
data = {items: new Array()};

data.items.push({value: 'item1'});
alert(data.items[0].value);
[/CODE]
Copy linkTweet thisAlerts:
@mahoniauthorNov 14.2010 — Yes, yes, yessssssssssssssssssssssssssssssssssssss.

You are my idollllllllllll!!

Thanks so much from spain!!!!!
Copy linkTweet thisAlerts:
@MikeOSNov 14.2010 — My pleasure from London ?
×

Success!

Help @mahoni 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 5.27,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...