0

我有一个嵌套的金融投资组合 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
                }
            ]
        }
    ]
}
  • 库存:(1000.50 * 20) = 20,010 美元
  • 债券 1:(100.99 * 4) = 403.96 美元
  • 债券 2:(530.50 * 2) = 1,061 美元
  • 债券总额:1,464.96 美元

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

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

4

3 回答 3

1

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 )

于 2021-03-18T21:55:38.187 回答
0

灵活解决方案的忠实拥护者,所以这里是一个使用对象扫描的解决方案

// const objectScan = require('object-scan');

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 }] }] };

const r = objectScan(['portfolio_accounts[*].holdings[*]'], {
  reverse: false,
  filterFn: ({
    value: { shares, price, security_type: securityType },
    context: { holdings, totals }
  }) => {
    const netWorth = shares * price;
    holdings.push({ [securityType]: netWorth });
    if (!(securityType in totals)) {
      totals[securityType] = 0;
    }
    totals[securityType] += netWorth;
  }
})(data, {
  holdings: [],
  totals: {}
});

console.log(r);
/* =>
{
  holdings: [ { Stock: 20010 }, { Bond: 403.96 }, { Bond: 1061 } ],
  totals: { Stock: 20010, Bond: 1464.96 }
}
 */
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan@14.0.0"></script>

免责声明:我是对象扫描的作者

于 2021-04-08T05:33:23.543 回答
0

您可以创建一个将所有可能的安全类型设置为 0 的对象,然后将每个持有量相加,例如

let 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
                }
            ]
        }
    ]
};
let security_types = {
  Bond: 0,
  Stock: 0,
  Crypto: 0
};
data.portfolio_accounts.forEach(portfolio_account => {
  portfolio_account.holdings.forEach(holding => {
    security_types[holding.security_type] += holding.price * holding.shares;
  });
});
console.log(security_types);

于 2021-03-18T21:30:13.510 回答