繁体   English   中英

如何JS object结构

[英]how to JS object structure

每个元素变成一个nullsfiscalYear: yearDatecurrentFy ...

{
   "item": "PCS COST",
   "currentFy": 1000,
   "currentFy01": 1000,
   "currentFy02": null,
   "currentFy03": null,
   "currentFy04": null,
   "currentFy05": null
},
{
    "item": "Travel",
    "currentFy": null,
    "currentFy01": null,
    "currentFy02": 10,
    "currentFy03": null,
    "currentFy04": 100,
    "currentFy05": null
}

至:

     [
            {
                
                "lineItem": "PCS COST",
                "lineItemAmount": 1000,
                "fiscalYear": 2022,
                
            },
            {
               
                "lineItem": "PCS COST",
                "lineItemAmount": 1000,
                "fiscalYear": 2023,
               
            },
            {
               
                "lineItem": "Travel",
                "lineItemAmount": 10,
                "fiscalYear": 2024,
               
            },
            {
                
                "lineItem": "Travel",
                "lineItemAmount": 100,
                "fiscalYear": 2026,
               
            }
        ]

代码: getCurrentFy 是获取年份的 function。

let row = [];

let initialObj = {};

 var i = 0;
 for (const { item } of inputState.fundingMatrix) {
      for (const { currentFy, currentFy01, currentFy03, currentFy04, currentFy05 } of inputState.fundingMatrix) {

     let value = currentFy && currentFy01 && currentFy03 && currentFy04 && currentFy05;

      row[i] ??= { lineItem: item, lineItemAmount: value, fiscalYear: getCurrentFy, ...initialObj };
       i++;         

        }

当前 OutPut

[
    {
        "lineItem": "PCS COST",
        "lineItemAmount": null,
        "fiscalYear": 2022
    },
    {
        "lineItem": "PCS COST",
        "lineItemAmount": null,
        "fiscalYear": 2022
    },
    {
        "lineItem": "PCS COST",
        "lineItemAmount": null,
        "fiscalYear": 2022
    },
    {
        "lineItem": "Travel",
        "lineItemAmount": null,
        "fiscalYear": 2022
    },
    {
        "lineItem": "Travel",
        "lineItemAmount": null,
        "fiscalYear": 2022
    },
    {
        "lineItem": "Travel",
        "lineItemAmount": null,
        "fiscalYear": 2022
    },
    {
        "lineItem": "Equipment",
        "lineItemAmount": null,
        "fiscalYear": 2022
    },
    {
        "lineItem": "Equipment",
        "lineItemAmount": null,
        "fiscalYear": 2022
    },
    {
        "lineItem": "Equipment",
        "lineItemAmount": null,
        "fiscalYear": 2022
    }
]
let row = [];
for (const { item, currentFy, currentFy01, currentFy02, currentFy03, currentFy04, currentFy05 } of inputState.fundingMatrix) {
    row.push({ lineItem: item, lineItemAmount: currentFy,   fiscalYear: getCurrentFy });
    row.push({ lineItem: item, lineItemAmount: currentFy01, fiscalYear: getCurrentFy01 });
    row.push({ lineItem: item, lineItemAmount: currentFy02, fiscalYear: getCurrentFy02 });
    row.push({ lineItem: item, lineItemAmount: currentFy03, fiscalYear: getCurrentFy03 });
    row.push({ lineItem: item, lineItemAmount: currentFy04, fiscalYear: getCurrentFy04 });
    row.push({ lineItem: item, lineItemAmount: currentFy05, fiscalYear: getCurrentFy05 });
}

let clean = row.filter(value => value.lineItemAmount !== null);

return clean

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM