SEO Forums: Your seo discussion forum  
Welcome, Unregistered.
You last visited: Today at 02:06 AM
Tags:



Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-27-2008, 07:22 AM
Steve
Guest
 
Posts: n/a
Default Adding variables to object as name-value pairs



The currency and the value are read from an xml file. I would then
like to add them to an object. I have tried various permutations of
the following (ie using square brackets instead of dot etc) without
success.

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
var doc = document.implementation.createDocument('', 'dummy', null);
doc.load('CEBrates.xml');
var rateObject = {};
doc.onload = function () {
var cubes = doc.getElementsByTagNameNS('http://www.ecb.int/
vocabulary/2002-08-01/eurofxref','Cube');
var date = cubes[1].getAttribute('time');
document.write("Data for " + date + "<br /><br />")
for (var i = 2; i < cubes.length; i++){
var currency = cubes[i].getAttribute('currency');
var rate = cubes[i].getAttribute('rate');
rateObject.currency = rate;
}
document.write(rateObject.USD);
document.close()
};
</script>

I would be grateful for any help.

<steve.
Reply With Quote


  #2 (permalink)  
Old 09-27-2008, 07:23 AM
Conrad Lender
Guest
 
Posts: n/a
Default Re: Adding variables to object as name-value pairs

On 2008-09-26 22:23, Steve wrote:
> The currency and the value are read from an xml file. I would then
> like to add them to an object. I have tried various permutations of
> the following (ie using square brackets instead of dot etc) without
> success.

....
> for (var i = 2; i < cubes.length; i++){
> var currency = cubes[i].getAttribute('currency');
> var rate = cubes[i].getAttribute('rate');
> rateObject.currency = rate;


You are setting the "currency" property of the rateObject object, not
the "USD" or "GBP" properties. What you probably want as a result is
something like this (I didn't look too closely):

{
'USD': '123.456',
'GBP': '234.345',
'EUR': '345.456',
(etc)
}

Try using this instead:

rateObject[currency] = rate;

That's assuming that all the expected elements and attributes actually
exist and are not empty. Also, the values will be Strings, not Numbers,
but I guess you know that. Just in case :-)


- Conrad
Reply With Quote


  #3 (permalink)  
Old 09-27-2008, 07:23 AM
slebetman
Guest
 
Posts: n/a
Default Re: Adding variables to object as name-value pairs

On Sep 27, 4:23*am, Steve <stephen.jo...@googlemail.com> wrote:
> the following (ie using square brackets instead of dot etc) without
> success.
> <snip>
> * * * * var currency = cubes[i].getAttribute('currency');
> * * * * var rate = cubes[i].getAttribute('rate');
> * * * * rateObject.currency = rate;


/* what you probably want is:
*/

rateObject[currency] = rate;

/* so that later you can use something like:
*/

var usdRate = rateObject.USD;
var myrRate = rateObject.MYR;
Reply With Quote


Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 02:06 AM.

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
Great Seo Blog at SEONOTEPAD.COM