简体   繁体   English

挖掘子对象并重建数据

[英]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.

相关问题 在包含嵌套数组的javascript中重建对象数组 - Reconstruct an array of objects in javascript that contains nested arrays jQuery数据方法将数据推送到子对象 - jQuery data method pushes data to child objects 如何找出页面上正在使用的 javascript 对象? (挖掘 webpack 捆绑包) - how can I find out what javascript objects are in use on a page? (digging through a webpack bundle) 如何从Chrome重建POST数据? - How to reconstruct POST data from Chrome? 处理来自ElasticSearch的对象-我需要重建它吗? - Dealing with Objects from ElasticSearch - Do I need to reconstruct it? js / reactjs-如何将json中的数据重构为数组? - js/reactjs - How do i reconstruct data from a json into a array? 通过唯一字段合并 JSON 对象,然后打印子节点数据 - Merge JSON objects through unique field then print child node data 使用 Javascript 我如何将这个对象数组解构和重建为 json 对象 - Using Javascript how would I deconstruct and reconstruct this array of objects to json objects 向用户询问新数据并将其添加到带有子对象的数组中 - Ask new data to the user and add it in to array with child objects React,错误:对象作为 React 子项无效(找到:object 和键 {data}) - React , Error: Objects are not valid as a React child (found: object with keys {data})
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM