2

I have json data that is structured like this:

{
  "timestamp": 1467471622,
  "base": "USD",
  "rates": {
    "AED": 3.673027,
    "AFN": 68.475,
    "ALL": 123.095199,
    "AMD": 476.8075,
    "ANG": 1.78385,
    "AOA": 165.846832,
    "ARS": 15.05143,
    "AUD": 1.333463,
    "AWG": 1.793333,
    "AZN": 1.553975,
    "BAM": 1.757679,
    "BBD": 2,
    "BDT": 78.33184,
    "BGN": 1.756683,
    "BHD": 0.377337,
    "BIF": 1660.642515,
    "BMD": 1,
    "BND": 1.344589,

How can I map this to muliple objects in typescript like this:

export interface Stock {
  name: string;
  value: number;
}

Thanks

2
  • What exactly do you mean? The json data doesn't have the stock name in it and by value do you just want the price in USD? What I would do is write some interfaces that represent the json data's structure, then write some interfaces for how I want to use the data in the application, then write some code to map the data between them. Commented Jul 2, 2016 at 16:23
  • 1
    Actually, this data seems to represent currency exchange rates. I see that base is USD and at the moment 1 USD ≈ 1.33 AUD. Might not be good to call it Stock then. Commented Jul 2, 2016 at 16:27

2 Answers 2

2
let keys = Object.keys(data.rates);
let mapped: Stock[] = keys.map(key => {
    return { name:key, value: data.rates[key] } as Stock
});

console.log(mapped);

https://jsfiddle.net/qfo43o24

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

Comments

1

You donot need to map manually. Your Typescript code can be:

export interface Stock{
  timestamp : Number,
  base : String,
  rates : Rates
}

export class Rates{
  AED : Number,
 ..... so on

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.