【问题标题】:Multidimensional Object to array in Angular 8Angular 8中的多维对象数组
【发布时间】:2019-12-25 22:14:51
【问题描述】:

我想向 *ngFor 发送数据。我从 LocalStorage 获取数据作为 JSON 对象。解析对象后,我不明白如何将完整的对象制作成数组?这是对象:

{
  "Monti": {
    "name": "Visa & MasterCard",
    "code": "monti",
    "deposit": "Free",
    "depositProcessing": "Instant",
    "icon": "<img src=\"//cdn.example.com/_payicons/monti.png\"/>",
    "supportWithdrawal": true,
    "withdrawAllowedWithoutDeposit": false,
    "depositLimits": {
      "currency": "EUR",
      "min": 10,
      "max": 5000
    }
  },
  "Montii": {
    "name": "Visa & MasterCard",
    "code": "monti",
    "deposit": "Free",
    "depositProcessing": "Instant",
    "icon": "<img src=\"//cdn.example.com/_payicons/monti.png\"/>",
    "supportWithdrawal": true,
    "withdrawAllowedWithoutDeposit": false,
    "depositLimits": {
      "currency": "EUR",
      "min": 10,
      "max": 5000
    }
  }
}

【问题讨论】:

    标签: arrays json angular typescript object


    【解决方案1】:

    您有一个包含两个其他对象的对象。我认为你想要的是一个对象数组,它们会像这样:

    [
        {
            "name": "Visa & MasterCard",
            "code": "monti",
            "deposit": "Free",
            "depositProcessing": "Instant",
            "icon": "",
            "supportWithdrawal": true,
            "withdrawAllowedWithoutDeposit": false,
            "depositLimits": {
                "currency": "EUR",
                "min": 10,
                "max": 5000
            }
        },
        {
            "name": "Visa & MasterCard",
            "code": "monti",
            "deposit": "Free",
            "depositProcessing": "Instant",
            "icon": "",
            "supportWithdrawal": true,
            "withdrawAllowedWithoutDeposit": false,
            "depositLimits": {
                "currency": "EUR",
                "min": 10,
                "max": 5000
            }
        }
    ]
    

    您只需要在将其保存到localStorage 时对其进行修复。如果不可能,您可以在该对象上运行“for in”循环以从中创建一个数组。

    let arr = [];
    for(let key in obj) {
      arr.push(obj[key]);
    }
    

    【讨论】:

    • 谢谢你 - 这行得通。但是有一个问题,为什么我不使用 Obj[value]?
    • 因为你通过对象的键而不是值本身获得一些值。
    猜你喜欢
    • 1970-01-01
    • 2015-12-30
    • 2019-08-03
    • 2018-09-10
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    相关资源
    最近更新 更多