PDA

View Full Version : convert object to number


grizjr
07-28-2004, 08:10 AM
I have an array of objects for the dataprovider for mm's components. Each object in the array has a 'Price' property that holds a price. When I try to add these values together it gives me 'NaN', even when using the Number() function.

Here's an example:


var test = new Array();
test[0] = new Object();
test[1] = new Object();
test[0].Price = 12.67;
test[1].Price = 56.66;

var total:Number;
for(t in test) {
total += Number(test[t].Price);
}

trace(total);


This returns 'NaN'. I need to add the prices together. Any ideas?

THanks :)

Crismo
07-28-2004, 09:21 AM
change var total:Number; to var total:Number = 0;
or var total:Number = new Number();

grizjr
07-28-2004, 09:57 AM
I tried both and still 'NaN'. :( Any other suggestions?

Crismo
07-28-2004, 10:05 AM
Hmm
That's weird. I'm running your code, but with my change and it works just as it should. Even the Debugger is ok with it.

You are running code that looks exactly like :

var test = new Array();
test[0] = new Object();
test[1] = new Object();
test[0].Price = 12.67;
test[1].Price = 56.66;

var total:Number = new Number();
for(t in test) {
total += Number(test[t].Price);
}

trace(total);

grizjr
07-28-2004, 10:34 AM
Hmm.. That's really weird, the code above does work. I took a shortcut and applied it to my function instead of using the above code. It still won't budge on my function though.

The only thing I can think of why is because the array of objects was serialized from an xml file.

I still can't get it to work... :(

This really sucks, the simpliest things take FOREVER.

The xml..

<products>
<product>
<price>34.22</price>
</product>
<product>
<price>65.44</price>
</product>
</products>


The AS...

var testXML:XML = new XML();
var testArr:Array= new Array();

testXML.ignoreWhite = true;
testXML.load('products.xml');
testXML.onLoad = function(ok) {
if(ok) {
var root= this.firstChild.childNodes;

for(y in root) {
var productNode = root[y].childNodes;

for(x in productNode) {
testArr[y] = new Object();
var item = productNode[x].nodeName;
var value = productNode[x].childNodes;
testArr[y][item] = value;
}
}
}
}



The above is how I am creating the array of objects. Does this shed some mroe light? :confused:

Crismo
07-28-2004, 11:46 AM
You where puting a xml node where your price value was supposed to go.
The following changes will fix the problem.


var testXML:XML = new XML();
var testArr:Array= new Array();

testXML.ignoreWhite = true;
testXML.load('test.xml');
testXML.onLoad = function(ok) {
if(ok) {
var root= this.firstChild.childNodes;

for(y in root) {
var productNode = root[y].childNodes;

for(x in productNode) {
var item = productNode[x].nodeName;
var value = productNode[x].firstChild.nodeValue;
testArr[y] = {Item:item, Price:Number(value)};
}
}

//Test ----------------
trace(testArr[0].Item + " : " + testArr[0].Price );
trace(testArr[1].Item + " : " + testArr[1].Price );
trace("Sum : " + (testArr[0].Price + testArr[1].Price));
//Test-------------------
}
}

grizjr
07-28-2004, 12:39 PM
THANK YOU, I've been struggling with that for weeks now. :eek: