/    Sign up×
Community /Pin to ProfileBookmark

Question regarding javascript Array

I understand that javascript array can be used as a hash table object.

However, I’m having troubles with the following code:

var test = new Array();

function loveClick(display) {
test[display] = something
// do something
}
}

Even for a simple action:
test[‘one’] = 1, it works outside of the function but not inside.

Sorry for being a newbie, but any suggestions on what’s going on and what needs to be done to get array working in a function please?

Thanks!

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@KorApr 17.2008 — You make a confusion. Your object is not a hash table, it is an array, which is not the same.

This is an object (or record, struct, dictionary, hash table, keyed list, associative array):
<i>
</i>var myobj= new Object();
//or (JSON)
var myobj={};

This is an array (a vector, list, sequence):
<i>
</i>var myarr = new Array();
//or (JSON)
var myarr=[];


An object is an [I]unordered[/I] collection of [B]name(property)/value pairs[/B].

An array is an [I]ordered[/I] list of values.
<i>
</i>var myobj={
one:1,
two:2,
three:3
}
var myarr=[
1,
2,
3
]
×

Success!

Help @ianlin0126 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.19,
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,
)...