0

I have the following JavaScript object, with a property called rates, which contains another object with the actual rates. Now I want to add a new rate "CAD":0.972254 to the rates. How can I add this one value to the list?

var Currency = {
  rates: {"USD":1.0,"EUR":1.3497,"GBP":1.60403},
  convert: function(amount, from, to) {
    return (amount * this.rates[from]) / this.rates[to]; 
  }
};
2
  • Well... Currency.rates.CAD = 0.972254 Just like you'd do with any object property access. Commented Sep 25, 2013 at 13:39
  • Try Currency.rates.CAD = 0.972254 :) Commented Sep 25, 2013 at 13:40

3 Answers 3

1

You can do it either way

Currency['rates']['CAD'] = 0.972254;

Or,

Currency.rates.CAD = 0.972254

Fiddle Here

Sign up to request clarification or add additional context in comments.

Comments

1

You can assign a new property to existing object like:

Currency.rates.CAD = 0.972254 

Comments

1

Currency.rates.CAD = 0.972254;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.