[英]digging into child objects and reconstruct the data
I've having currencies list in JSON that comes like this我在 JSON 中有这样的货币列表
{
"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我想格式化 output 所以我得到它这样
[{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)
})
}
The following code should help you.以下代码应该对您有所帮助。
const formattedData = Object.values(this.currenciesList).map(({ name, symbol }) => ({ name, symbol }))
console.log(formattedData)
Here is one approach:这是一种方法:
const data = { "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" }, } const result = Object.values(data).map(({ name, symbol }) => ({ name, symbol })) console.log(result)
loadCurrencies() {
this.http.get('assets/data/currencies.json').subscribe((response) => {
this.currenciesList = Object.values(response).map(obj =>
return {
name: obj.name,
symbol: obj.symbol
});
console.log(this.currenciesList);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.