Syed Ahmed
Syed Ahmed

Reputation: 209

How to insert entries from into a nested JavaScript Object

I'm facing trouble in making a nested insertion of a particular entry into my data structure. The 'positionValue' in the 'data' object below has to be inserted into 'mystructure' based on whether it is Team1 or Team2, and based on the category 'Lombard Loans/Time Deposits/Demand Deposits' and then further based on 'name' of the product (the last nested structure).

The original object:

data: [
        {
          balanceSheetPositionResults: [
            {
              positionValue: 12,
              balanceSheetPosition: {
                name: "asset_bc_lombard_a_onsight",
                category: "LOMBARD_LOANS",
                type: "ASSET"
              },    
            },
            {
              positionValue: 58,
              balanceSheetPosition: {
                name: "liability_bc_timedeposits",               
                category: "TIME_DEPOSITS",
                type: "ASSET"
              },              
            },
            {
              positionValue: 58,
              balanceSheetPosition: {
                name: "liability_bc_demanddeposits",
                category: "DEMAND_DEPOSITS",
                type: "ASSET"
              },   
            },
            {
              positionValue: 10,
              balanceSheetPosition: {
                name: "asset_bc_lombard_a_lt1m",
                category: "LOMBARD_LOANS",
                type: "ASSET"
              },   
            }
          ],
          bank: {
            name: "Team1"
          },
          game: {
            name: "TestGame"
          },
          bsSum: 2,
          period: {
            index: 1
          },
        },
        {
          balanceSheetPositionResults: [
            {
              positionValue: 12,
              balanceSheetPosition: {
                name: "asset_bc_lombard_a_onsight",
                category: "LOMBARD_LOANS",
                type: "ASSET"
              },  
            },
            {
              positionValue: 58,
              balanceSheetPosition: {
                name: "liability_bc_timedeposits",
                category: "TIME_DEPOSITS",
                type: "ASSET"
              },
            },
            {
              positionValue: 58,
              balanceSheetPosition: {
                name: "liability_bc_demanddeposits",
                category: "DEMAND_DEPOSITS",
                type: "ASSET"
              },          
            },
            {
              positionValue: 10,
              balanceSheetPosition: {
                name: "asset_bc_lombard_a_lt1m",
                category: "LOMBARD_LOANS",
                type: "ASSET"
              },    
            }
          ],      
          bank: {
            name: "Team2"
          },  
          game: {
            name: "TestGame"
          },
          bsSum: 2,
          period: {
            index: 1
          }
        }
      ]

The structure that I made after some transformation (this is just a snippet):

{ mystructure:
   [ 
   { name: 'Team2',
       LOMBARD_LOANS:
        [ { name: 'asset_bc_lombard_a_onsight'
          },
          { name: 'asset_bc_lombard_a_lt1m'
          } 
        ],
       DEMAND_DEPOSITS:
        [ { name: 'liability_bc_demanddeposits'
             }
        ],
       TIME_DEPOSITS:
        [ { name: 'liability_bc_timedeposits'
            }
        ]
    },
     { name: 'Team1',
       LOMBARD_LOANS:
        [ { name: 'asset_bc_lombard_a_onsight'
          },
          { name: 'asset_bc_lombard_a_lt1m'
          } 
        ],
       DEMAND_DEPOSITS:
        [ { name: 'liability_bc_demanddeposits'
             }
        ],
       TIME_DEPOSITS:
        [ { name: 'liability_bc_timedeposits'
            } 
        ] 
    } 
    ] 
}

The result that would look like:

{ mystructure:
   [ 
   { name: 'Team2',
       LOMBARD_LOANS:
        [ { name: 'asset_bc_lombard_a_onsight',
            positionValue: 12
          },
          { name: 'asset_bc_lombard_a_lt1m',
            positionValue: 58
          } 
        ],
       DEMAND_DEPOSITS:
        [ { name: 'liability_bc_demanddeposits',
            positionValue: 58
             }
        ],
       TIME_DEPOSITS:
        [ { name: 'liability_bc_timedeposits',
            positionValue: 10
            }
        ]
    },
     { name: 'Team1',
       LOMBARD_LOANS:
        [ { name: 'asset_bc_lombard_a_onsight',
            positionValue: 12
          },
          { name: 'asset_bc_lombard_a_lt1m',
            positionValue: 58
          } 
        ],
       DEMAND_DEPOSITS:
        [ { name: 'liability_bc_demanddeposits',
            positionValue: 58
             }
        ],
       TIME_DEPOSITS:
        [ { name: 'liability_bc_timedeposits',
            positionValue: 10
            } 
        ] 
    } 
    ] 
}

Upvotes: 1

Views: 46

Answers (1)

Easwar
Easwar

Reputation: 5422

Assuming each bank name comes only once, pass your original array to this transformer :

function transformData(data) {
  return data.map(entry => {
    const loanType = {};
    entry.balanceSheetPositionResults.forEach(balanceEntry => {
      const { name, category, type } = balanceEntry.balanceSheetPosition;
      if (!(category in loanType)) {
        loanType[category] = [];
      }

      loanType[category].push({
        name,
        positionValue: balanceEntry.positionValue
      });
    });
    return {
      name: entry.bank.name,
      ...loanType
    };
  });
}

Upvotes: 1

Related Questions