我有如下结构的json数据:

{
  "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,

如何将此映射到typescript中的多个对象,如下所示:
export interface Stock {
  name: string;
  value: number;
}

谢谢

最佳答案

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

07-25 20:42