/    Sign up×
Community /Pin to ProfileBookmark

Setting object property name from variable (or associative arrays)

Hi all,

I am facing the following problem:
I load a set of data from an XML file and I need to map this in JavaScript. Either in an associative array or in an object (you tell me which ?.

[CODE]var myArray = Array();
for(iCount = 0; iCount < rootNode.childNodes.length; iCount++) {
currentChild = rootNode.childNodes[kCount];
if(currentChild.nodeName.substring(0,1) != ‘#’) {
//Is a tag node (does not start with #)
varNodeName = currentChild.nodeName;
myArray[iCount] = Array();
if(currentChild.childNodes.length > 0) {
//Has a value (textnode child)
escalationData[iCount][varNodeName] = currentChild.firstChild.data;
} else {
//No children, set empty value
escalationData[iCount][varNodeName] = ”;
}
}
}[/CODE]

But what is happening is that every myArray[iCount] entry only has one property: the las one which has been set in the loop. So this line:

[CODE]escalationData[iCount][varNodeName] = currentChild.firstChild.data;[/CODE]

Is constantly overwriting the previously set value, although varNodeName always has a different value (as I tested by printing it within the loop).

Any hints on how to solve this situation? I thought about using objects, but how do I set a propertyname using avalue?
myArray.variableName does not work, obviously.

Any hints greatly appreciated!

Cheers

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@moenschauthorApr 18.2007 — Sorry guys, but I am being a bit thick today ?

Obviously, it does not help to re-initialize the array at every iteration.

Took me the best parts of the last 2 hours to find this.. ARGHL!
×

Success!

Help @moensch 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.24,
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,
)...