简体   繁体   中英

how to JS object structure

Each Element becomes an individual object and currentFy ... become the fiscalYear: yearDate and not ADD nulls Change:

{
   "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
}

To:

     [
            {
                
                "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,
               
            }
        ]

code: getCurrentFy is a function to get year.

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++;         

        }

current 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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