首页 > 解决方案 > JavsScript - 遍历嵌套数组

问题描述

我有一个嵌套的金融投资组合 JSON 文件。我需要从每个持有的所有投资组合帐户中获取 security_type(在示例 JSON 中只有一个),以及每个投资组合的每个个人持有的总净值,所有这些都在一个循环中。

最终,我尝试使用这些信息在饼图中显示,该饼图根据证券类型的净值进行分离。

JSON文件:

{
    "id": 1,
    "username": "Test",
    "portfolio_accounts": [
        {
            "id": 1,
            "user": 1,
            "username": "Test",
            "account_type": "IRA",
            "name": "MyTestAccount",
            "description": "Just a Test",
            "balance": 100.00,
            "holdings": [
                {
                    "id": 1,
                    "portfolio_id": 2,
                    "security_type": "Stock",
                    "ticker": "GOOG",
                    "price": 1000.50,
                    "shares": 20,
                    "purchase_date": "02-20-2021",
                    "cost_basis": 800.50
                }, 
                {
                    "id": 2,
                    "portfolio_id": 2,
                    "security_type": "Bond",
                    "ticker": "AMZN",
                    "price": 100.99,
                    "shares": 4,
                    "purchase_date": "02-20-2021",
                    "cost_basis": 60.65
                }
            ]
        },
        {
            "id": 2,
            "user": 1,
            "username": "Test",
            "account_type": "IRA",
            "name": "MyTestAccount2 - Electric Boogaloo",
            "description": "Repeat",
            "balance": 100.00,
            "holdings": [
                {
                    "id": 3,
                    "portfolio_id": 3,
                    "security_type": "Bond",
                    "ticker": "GNMA",
                    "price": 530.50,
                    "shares": 2,
                    "purchase_date": "02-20-2021",
                    "cost_basis": 40.20
                }
            ]
        }
    ]
}

此 JSON 的预期饼图输出示例:

一个饼图,其中一种颜色代表债券(1,464.96 美元),另一种颜色代表股票(20,010 美元),按比例填充。如果有其他安全类型,比如加密,我需要做同样的事情并自动添加第三种颜色(依此类推)。

标签: javascriptarraysjsonmultidimensional-arrayjavascript-objects

解决方案


const data = 
  { id       : 1
  , username : 'Test'
  , portfolio_accounts: 
    [ { id           : 1
      , user         : 1
      , username     : 'Test'
      , account_type : 'IRA'
      , name         : 'MyTestAccount'
      , description  : 'Just a Test'
      , balance      : 100.00
      , holdings: 
        [ { id            : 1
          , portfolio_id  : 2
          , security_type : 'Stock'
          , ticker        : 'GOOG'
          , price         : 1000.50
          , shares        : 20
          , purchase_date : '02-20-2021'
          , cost_basis    : 800.50
          } 
        , { id            : 2
          , portfolio_id  : 2
          , security_type : 'Bond'
          , ticker        : 'AMZN'
          , price         : 100.99
          , shares        : 4
          , purchase_date : '02-20-2021'
          , cost_basis    : 60.65
      } ] } 
    , { id           : 2
      , user         : 1
      , username     : 'Test'
      , account_type : 'IRA'
      , name         : 'MyTestAccount2 - Electric Boogaloo'
      , description  : 'Repeat'
      , balance      : 100.00
      , holdings: 
        [ { id            : 3
          , portfolio_id  : 3
          , security_type : 'Bond'
          , ticker        : 'GNMA'
          , price         : 530.50
          , shares        : 2
          , purchase_date : '02-20-2021'
          , cost_basis    : 40.20
  } ] } ] } 
, security_types = { Stock : 0, Bond : 0 }
  ;
for(let ptfAcc of data.portfolio_accounts ) 
for(let hld    of ptfAcc.holdings )
  security_types[hld.security_type] += (hld.price * hld.shares)
  ;
console.log( security_types )


推荐阅读