Skip to main content
February 4, 2007
Question

Returning an array of objects from a function

  • February 4, 2007
  • 1 reply
  • 408 views
Hi,

I am trying to return an array of objects from a function that reads in an xml file. Everytime I try to do a trace on populationData I get undefined.

I have a similar function that returns an array of values that works fine i.e.

list = myValue; instead of list.addItem(myRow);

but when i try to create an array of objects it doesnt work, if anyone could tell where I am going wrong I would be most grateful, here is my code:

/**
* Load imported xml values into the application variables
*/
loadXMLValues = function(file) : Void {
var populationData = new Array();
var myXML:XML = new XML();
myXML.ignoreWhite = true;
myXML.onLoad = function(success) {
if (success) {
populationData = importData(myXML);
trace(populationData);
} else {
// Error
}
}
myXML.load(file);
}

/**
* Return an array of demographic values from xml file
*/
importData = function(myXML): Array {
var list:Array = new Array();
for (n=0; n<myXML.firstChild.childNodes.length; n++) {
var var1:String = myXML.firstChild.childNodes.firstChild.firstChild.nodeValue;
var var2:String = myXML.firstChild.childNodes.firstChild.nextSibling.firstChild.nodeValue;
var var3:String = myXML.firstChild.childNodes.firstChild.nextSibling.nextSibling.firstChild.nodeValue;
var var4:String = myXML.firstChild.childNodes.firstChild.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;
var var5:String = myXML.firstChild.childNodes.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;
var var6:String = myXML.firstChild.childNodes.lastChild.firstChild.nodeValue;
var myRow:Object = {Var1:var1,
Var2:var2,
Var3:var3,
Var4:var4,
Var5:var5,
Var6:var6};
list.addItem(myRow);
}
return list;
}
This topic has been closed for replies.

1 reply

February 5, 2007
Hi Guys,

still working on this, the bizzare thing is I can't even get a value whilst in the function like this:

trace(list[1].Var1);
trace(list[1].Var2);

Can anyone see why this is happening, I know it is reading in values from the xml because trace(var1) works fine...
Inspiring
February 5, 2007
Your myRow object is an associative array. So you need to access the elements not using the square brackets but with a dot. See code below;
February 5, 2007
Hi,

Thanks for your reply, that works fine, but I need to test whether each my row object is being added to list correctly, hence the code trace(list[1].Var1);

But it's still outputting undefined...