Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

javascript - digging into child objects and reconstruct the data

I've having currencies list in JSON that comes like this

{
    "USD": {
        "symbol": "$",
        "name": "US Dollar",
        "symbol_native": "$",
        "decimal_digits": 2,
        "rounding": 0,
        "code": "USD",
        "name_plural": "US dollars"
    },
    "CAD": {
        "symbol": "CA$",
        "name": "Canadian Dollar",
        "symbol_native": "$",
        "decimal_digits": 2,
        "rounding": 0,
        "code": "CAD",
        "name_plural": "Canadian dollars"
    },
}

I want to format the output so I get it like this

[{name: "US Dolloar", symbol: "$"}, {name: "Canadian Dolloar", symbol: "CA$"} ]

but I'm finding it hard to do

  loadCurrencies() {
    this.http.get('assets/data/currencies.json').subscribe((response) => {
      this.currenciesList = response;
      console.log(this.currenciesList)
    })
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following code should help you.

const formattedData = Object.values(this.currenciesList).map(({ name, symbol }) => ({ name, symbol }))

console.log(formattedData)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...