1

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

1 Answer 1

1

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
    };
  });
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Easwar, the code that you posted does convert it correctly to my structure but what I'm looking for is that it should insert the Positionvalue in 'mystructure' and not make a new one from the original data structure. 'mystructure' contains more information and the structure that I posted is a simplified version to make things clear.
Could you add how mystructure looks before this transformation ? with some sample keys
'mystructure' is a derived structure from the original 'data' structure. It contains information spread across various children from the 'data' structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.