/    Sign up×
Community /Pin to ProfileBookmark

Array question – why is element not being initialized.

I’m trying to set an array with elements not numbered, but referenced with names of my selection and values of the elements in the array with my choices.

Could someone advise me as to why I get an ‘undefined’ in the alert display after entering pairs of “names” and “values” with the “Add” button?
I don’t see any JS console errors, but I don’t seem to be able to set the elements either. Any ideas?

[code=php]
<html>
<head>
<title>Array Test</title>
<script type=”text/javascript”>

NameList = {
ArrLength : 0,
ItemArray : new Array(),

initItemArray : function() { this.ArrLength = 0; },
putItem : function(item,value) { this.ItemArray[item] = value; this.ArrLength++; },
getItem : function(item) { return this.ItemArray[item]; },

showItems : function() {
var t = ”;
for (var i in this.ItemArray) {
t += i+’ : ‘+this.getItem[i]+”n”;
}
alert(‘ItemArrayn’+t);
}
}
function AddItem() {
var n = document.getElementById(‘FName’).value
var v = document.getElementById(‘FValue’).value;
NameList.putItem(n,v);
}
function Clear() {
document.getElementById(‘FName’).value = ”;
document.getElementById(‘FValue’).value = ”;
}
function Show() { NameList.showItems(); }
</script>
</head>
<body>
<input type=”text” id=”FName” value=””>Name<br />
<input type=”text” id=”FValue” value=””>Value<br />
<button onClick=”AddItem()”>Add</button>
<button onClick=”Clear()”>Clear</button>
<button onClick=”Show()”>Show</button>
</body>
</html>
[/code]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@mudeltaMar 25.2007 —  showItems : function() {
var t = '';
for (var i in this.ItemArray) {
t += i+' : '+this.getItem[COLOR="red"][[/COLOR]i[COLOR="Red"]][/COLOR]+"n";
}
alert('ItemArrayn'+t);
}
[/quote]


--> this.getItem[COLOR="blue"]([/COLOR]i[COLOR="Blue"])[/COLOR]
Copy linkTweet thisAlerts:
@JMRKERauthorMar 26.2007 — Thanks "mudelta"

I guess if you look a problem long enough it doesn't go away!

I guess I'll go put on my dunce cap for awhile.
Copy linkTweet thisAlerts:
@JMRKERauthorMar 26.2007 — OK, new question on same topic.

Once I create an element of the array, how do I delete that element later?

For example:

array1 = new Array()

array1[0] = 'zero';

array1[1] = 'one';

array1[2] = 'two';

array1[3] = 'three'

will create a 4 element array and I can delete element #2 like this:

array1.splice(2,1);

But if I create the array like this:

array2 = new Array();

array2['zero'] = 0;

array2['one'] = 1;

array2['two'] = 2;

array2['three'] = 3

Will create another 4 element array, but I can't seem to delete element # 'two' using the syntax of array2.splice('two',1);

Does anyone have an idea how I can delete the array element if it is 'named' instead of 'numbered'?

Thanks again.
Copy linkTweet thisAlerts:
@KorMar 26.2007 — Your second array should be rather written as an [I]associative array (an object)[/I], not as a sorted array. If you don't believe, try alert your array2.length. You will have the length 0, that means your array is an associative array.

To remove a member(a property) of an object, simply use the [B]delete[/B] operator:
<i>
</i>var array2 = {
'zero': 0,
'one': 1,
'two': 2,
'three': 3
}

delete array2['two'];

for(var x in array2){
alert(array2[x]);
}
Copy linkTweet thisAlerts:
@JMRKERauthorMar 26.2007 — Thanks 'kor',

Seems to work just as I want it now.

Just an aside, are there any dangers to using an 'associative array' as opposed to using the number array.


I did not know they were referred to by such names.


(By the way, it was not intended to be sorted in the example, just worked out that way.)

Now you've got me to thinking (Danger, Danger Will Robinson!).

Could I use the sort function on the associative array like:
[code=php]
for (var i in sort array2) { print array2(i); }
[/code]

or would I need to create a different array to display sorted associative array like:
[code=php]
var tarr = new Array();
for (var i in array2) { tarr.push(i); }
tarr = sort tarr;
for (i=0; i<tarr.length; i++) { print array2(tarr[i]); }
[/code]


Appreciate the information.

jmrker
Copy linkTweet thisAlerts:
@KorMar 26.2007 — As somehow an unwritten rule, to circle through a sorted array you should use an indexed loop f[B]or(var i=0; i<arr.length;i++) [/B]- (or a backward loop, or a while loop). To circle through an associative array (through an objects members/properties) you'd better use an associative loop f[B]or(var [I]foo[/I] in [I]obj[/I])[/B] - (you may use a while loop as well, but not based on index, because there is no index within the object's properties);

There are huge differences between sorted and unsorted arrays (see also JSON.

Objects (and the collections of HTML elements, on the other hand) are not "classical" arrays. There are several differences.

It depends on your needs. You may create on object, and the values of it's properties may be arrays...
Copy linkTweet thisAlerts:
@JMRKERauthorMar 26.2007 — Thanks "kor"
×

Success!

Help @JMRKER 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

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