Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

How to insert an array into a nested JavaScript Object

The object i am currently using looks like this

const data = {
  periods: [
    {
      decisions: [
        {
          bank: {
            name: "Team1"
          },
          bSPositionDecisions: [
            {
              totalInputRate: 1.0,
              balanceSheetPosition: {
                name: "asset_bc_lombard_a_onsight",
                category: "LOMBARD_LOANS",
                type: "ASSET"
              }
            },
            {
              totalInputRate: 2.0,
              balanceSheetPosition: {
                name: "asset_bc_lombard_a_lt1m",
                category: "LOMBARD_LOANS",
                type: "ASSET"
              }
            }
          ]
        },
        {
          bank: {
            name: "Team2"
          },

          bSPositionDecisions: [
            {
              totalInputRate: 5.0,
              balanceSheetPosition: {
                name: "asset_bc_lombard_a_onsight",
                category: "LOMBARD_LOANS",
                type: "ASSET"
              }
            },
            {
              totalInputRate: 6.0,
              balanceSheetPosition: {
                name: "asset_bc_lombard_a_lt1m",
                category: "LOMBARD_LOANS",
                type: "ASSET"
              }
            }
          ]
        }
      ],
      settings: {
        regularCreditCBrate: 0.5,
        bSPositionSettings: [
          {
            bsPosition: {
              name: "asset_bc_lombard_a_onsight",
              category: "LOMBARD_LOANS",
              type: "ASSET"
            },
            mktTplus1GrowthPercentage: 0.5,
            atrFeatures: [
              {
                inputValues: [
                  {
                    bank: {
                      name: "Team1"
                    },
                    inputValue: 0.67
                  },
                  {
                    bank: {
                      name: "Team2"
                    },
                    inputValue: 0.23
                  }
                ],
                atrMSettings: {
                  name: "Attracting_clients_asset_side",
                  curve: {
                    name: "Attracting_clients_asset_side",
                    ccid: 7
                  },
                  wgt: 0.3
                }
              },
              {
                inputValues: [
                  {
                    bank: {
                      name: "Team1"
                    },
                    inputValue: 0.5
                  },
                  {
                    bank: {
                      name: "Team2"
                    },
                    inputValue: 0.5
                  }
                ],
                atrMSettings: {
                  name: "GDP_growth_on_loans",
                  curve: {
                    name: "BIP_growth_on_loans",
                    ccid: 8
                  },
                  wgt: 0.5
                }
              },
            ],
            chrFeatures: [
              {
                inputValues: [
                  {
                    bank: {
                      name: "Team1"
                    },
                    inputValue: 0.58
                  },
                  {
                    bank: {
                      name: "Team2"
                    },
                    inputValue: 0.68
                  }
                ],
                chrMSettings: {
                  name: "Sensitive_churning_clients_asset_side",
                  curve: {
                    name: "Sensitive_churning_clients_asset_side",
                    ccid: 1
                  },
                  min: 0.5,
                  max: 0.6,
                  wgt: 0.5
                }
              },
              {
                inputValues: [
                  {
                    bank: {
                      name: "Team1"
                    },
                    inputValue: 0.6
                  },
                  {
                    bank: {
                      name: "Team2"
                    },
                    inputValue: 0.6
                  }
                ],
                chrMSettings: {
                  name: "Service_quality_index",
                  curve: {
                    name: "Service_quality_asset_side",
                    ccid: 3
                  },
                  min: 0.5,
                  max: 0.6,
                  wgt: 0.5
                }
              }
            ],
            multiple: 0.5,
            hqlaMultiple: 0.5
          },
          {
            bsPosition: {
              name: "asset_bc_lombard_a_lt1m",
              category: "LOMBARD_LOANS",
              type: "ASSET"
            },
            mktTplus1GrowthPercentage: 0.5,
            atrFeatures: [
              {
                inputValues: [
                  {
                    bank: {
                      name: "Team1"
                    },
                    inputValue: 0.5
                  },
                  {
                    bank: {
                      name: "Team2"
                    },
                    inputValue: 0.5
                  }
                ],
                atrMSettings: {
                  name: "Attracting_clients_asset_side",
                  curve: {
                    name: "Attracting_clients_asset_side",
                    ccid: 7
                  },
                  wgt: 0.5
                }
              },
              {
                inputValues: [
                  {
                    bank: {
                      name: "Team1"
                    },
                    inputValue: 0.5
                  },
                  {
                    bank: {
                      name: "Team2"
                    },
                    inputValue: 0.5
                  }
                ],
                atrMSettings: {
                  name: "GDP_growth_on_loans",
                  curve: {
                    name: "BIP_growth_on_loans",
                    ccid: 8
                  },
                  wgt: 0.5
                }
              },           
            ],
            chrFeatures: [
              {
                inputValues: [
                  {
                    bank: {
                      name: "Team1"
                    },
                    inputValue: 0.6
                  },
                  {
                    bank: {
                      name: "Team2"
                    },
                    inputValue: 0.6
                  }
                ],
                chrMSettings: {
                  curve: {
                    name: "Sensitive_churning_clients_asset_side",
                    ccid: 1
                  },
                  min: 0.5,
                  max: 0.6,
                  wgt: 0.5
                }
              },
              {
                inputValues: [
                  {
                    bank: {
                      name: "Team1"
                    },
                    inputValue: 0.6
                  },
                  {
                    bank: {
                      name: "Team2"
                    },
                    inputValue: 0.6
                  }
                ],
                chrMSettings: {
                  name: "Service_quality_index",
                  curve: {
                    name: "Service_quality_asset_side",
                    ccid: 3
                  },
                  min: 0.5,
                  max: 0.6,
                  wgt: 0.5
                }
              }
            ],
            multiple: 0.5,
            hqlaMultiple: 0.5
          },
          {
            bsPosition: {
              name: "liability_bc_demanddeposits",
              category: "DEMAND_DEPOSITS",
              type: "LIABILITY"
            },
            mktTplus1GrowthPercentage: 0.5,
            atrFeatures: [
              {
                inputValues: [
                  {
                    bank: {
                      name: "Team1"
                    },
                    inputValue: 0.5
                  },
                  {
                      name: "Team2"
                    },
                    inputValue: 0.5
                  }
                ],
                atrMSettings: {
                  name: "Attracting_clients_asset_side",
                  curve: {
                    name: "Attracting_clients_asset_side",
                    ccid: 7
                  },
                  wgt: 0.5
                }
              },
              {
                inputValues: [
                  {
                    bank: {
                      name: "Team1"
                    },
                    inputValue: 0.5
                  },
                  {
                    bank: {
                      name: "Team2"
                    },
                    inputValue: 0.5
                  }
                ],
                atrMSettings: {
                  name: "GDP_growth_on_loans",
                  curve: {
                    name: "BIP_growth_on_loans",
                    ccid: 8
                  },
                  wgt: 0.5
                }
              }            
            ],
            chrFeatures: [
              {
                inputValues: [
                  {
                    bank: {
                      name: "Team1"
                    },
                    inputValue: 0.658
                  },
                  {
                    bank: {
                      name: "Team2"
                    },
                    inputValue: 0.987
                  }
                ],
                chrMSettings: {
                  name: "Sensitive_churning_clients_asset_side",
                  curve: {
                    name: "Sensitive_churning_clients_asset_side",
                    ccid: 1
                  },
                  min: 0.5,
                  max: 0.6,
                  wgt: 0.5
                }
              },
              {
                inputValues: [
                  {
                    bank: {
                      name: "Team1"
                    },
                    inputValue: 0.6
                  },
                  {
                    bank: {
                      name: "Team2"
                    },
                    inputValue: 0.6
                  }
                ],
                chrMSettings: {
                  name: "Service_quality_index",
                  curve: {
                    name: "Service_quality_asset_side",
                    ccid: 3
                  },
                  min: 0.5,
                  max: 0.6,
                  wgt: 0.5
                }
              }
            ],
            multiple: 0.5,
            hqlaMultiple: 0.5
          },
          {
            bsPosition: {
              name: "liability_bc_timedeposits", 
              category: "TIME_DEPOSITS",
              type: "LIABILITY"
            },
            mktTplus1GrowthPercentage: 0.5,
            atrFeatures: [
              {
                inputValues: [
                  {
                    bank: {
                      name: "Team1"
                    },
                    inputValue: 0.5
                  },
                  {
                    bank: {
                      name: "Team2"
                    },
                    inputValue: 0.5
                  }
                ],
           

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We can use map, filter reduce for this, it's slightly tricky but the same principle applies:

const  data  =  {  periods:  [  {  decisions:  [  {  bank:  {  name:  "Team1"  },  bSPositionDecisions:  [  {  totalInputRate:  1.0, positionValue: 12, balanceSheetPosition:  {  name:  "asset_bc_lombard_a_onsight",  category:  "LOMBARD_LOANS",  type:  "ASSET"  }  },  {  totalInputRate:  2.0,  positionValue: 10, balanceSheetPosition:  {  name:  "asset_bc_lombard_a_lt1m",  category:  "LOMBARD_LOANS",  type:  "ASSET"  }  }  ]  },  {  bank:  {  name:  "Team2"  },  bSPositionDecisions:  [  {  totalInputRate:  5.0,  positionValue: 12, balanceSheetPosition:  {  name:  "asset_bc_lombard_a_onsight",  category:  "LOMBARD_LOANS",  type:  "ASSET"  }  },  {  totalInputRate:  6.0,  positionValue: 10, balanceSheetPosition:  {  name:  "asset_bc_lombard_a_lt1m",  category:  "LOMBARD_LOANS",  type:  "ASSET"  }  }  ]  }  ],  settings:  {  regularCreditCBrate:  0.5,  bSPositionSettings:  [  {  bsPosition:  {  name:  "asset_bc_lombard_a_onsight",  category:  "LOMBARD_LOANS",  type:  "ASSET"  },  mktTplus1GrowthPercentage:  0.5,  atrFeatures:  [  {  inputValues:  [  {  bank:  {  name:  "Team1"  },  inputValue:  0.67  },  {  bank:  {  name:  "Team2"  },  inputValue:  0.23  }  ],  atrMSettings:  {  name:  "Attracting_clients_asset_side",  curve:  {  name:  "Attracting_clients_asset_side",  ccid:  7  },  wgt:  0.3  }  },  {  inputValues:  [  {  bank:  {  name:  "Team1"  },  inputValue:  0.5  },  {  bank:  {  name:  "Team2"  },  inputValue:  0.5  }  ],  atrMSettings:  {  name:  "GDP_growth_on_loans",  curve:  {  name:  "BIP_growth_on_loans",  ccid:  8  },  wgt:  0.5  }  },  ],  chrFeatures:  [  {  inputValues:  [  {  bank:  {  name:  "Team1"  },  inputValue:  0.58  },  {  bank:  {  name:  "Team2"  },  inputValue:  0.68  }  ],  chrMSettings:  {  name:  "Sensitive_churning_clients_asset_side",  curve:  {  name:  "Sensitive_churning_clients_asset_side",  ccid:  1  },  min:  0.5,  max:  0.6,  wgt:  0.5  }  },  {  inputValues:  [  {  bank:  {  name:  "Team1"  },  inputValue:  0.6  },  {  bank:  {  name:  "Team2"  },  inputValue:  0.6  }  ],  chrMSettings:  {  name:  "Service_quality_index",  curve:  {  name:  "Service_quality_asset_side",  ccid:  3  },  min:  0.5,  max:  0.6,  wgt:  0.5  }  }  ],  multiple:  0.5,  hqlaMultiple:  0.5  },  {  bsPosition:  {  name:  "asset_bc_lombard_a_lt1m",  category:  "LOMBARD_LOANS",  type:  "ASSET"  },  mktTplus1GrowthPercentage:  0.5,  atrFeatures:  [  {  inputValues:  [  {  bank:  {  name:  "Team1"  },  inputValue:  0.5  },  {  bank:  {  name:  "Team2"  },  inputValue:  0.5  }  ],  atrMSettings:  {  name:  "Attracting_clients_asset_side",  curve:  {  name:  "Attracting_clients_asset_side",  ccid:  7  },  wgt:  0.5  }  },  {  inputValues:  [  {  bank:  {  name:  "Team1"  },  inputValue:  0.5  },  {  bank:  {  name:  "Team2"  },  inputValue:  0.5  }  ],  atrMSettings:  {  name:  "GDP_growth_on_loans",  curve:  {  name:  "BIP_growth_on_loans",  ccid:  8  },  wgt:  0.5  }  },  ],  chrFeatures:  [  {  inputValues:  [  {  bank:  {  name:  "Team1"  },  inputValue:  0.6  },  {  bank:  {  name:  "Team2"  },  inputValue:  0.6  }  ],  chrMSettings:  {  curve:  {  name:  "Sensitive_churning_clients_asset_side",  ccid:  1  },  min:  0.5,  max:  0.6,  wgt:  0.5  }  },  {  inputValues:  [  {  bank:  {  name:  "Team1"  },  inputValue:  0.6  },  {  bank:  {  name:  "Team2"  },  inputValue:  0.6  }  ],  chrMSettings:  {  name:  "Service_quality_index",  curve:  {  name:  "Service_quality_asset_side",  ccid:  3  },  min:  0.5,  max:  0.6,  wgt:  0.5  }  }  ],  multiple:  0.5,  hqlaMultiple:  0.5  },  {  bsPosition:  {  name:  "liability_bc_demanddeposits",  category:  "DEMAND_DEPOSITS",  type:  "LIABILITY"  },  mktTplus1GrowthPercentage:  0.5,  atrFeatures:  [  {  inputValues:  [  {  bank:  {  name:  "Team1"  },  inputValue:  0.5  },  {  name:  "Team2"  },  {  inputValue:  0.5  }  ],  atrMSettings:  {  name:  "Attracting_clients_asset_side",  curve:  {  name:  "Attracting_clients_asset_side",  ccid:  7  },  wgt:  0.5  }  },  {  inputValues:  [  {  bank:  {  name:  "Team1"  },  inputValue:  0.5  },  {  bank:  {  name:  "Team2"  },  inputValue:  0.5  }  ],  atrMSettings:  {  name:  "GDP_growth_on_loans",  curve:  {  name:  "BIP_growth_on_loans",  ccid:  8  },  wgt:  0.5  }  }  ],  chrFeatures:  [  {  inputValues:  [  {  bank:  {  name:  "Team1"  },  inputValue:  0.658  },  {  bank:  {  name:  "Team2"  },  inputValue:  0.987  }  ],  chrMSettings:  {  name:  "Sensitive_churning_clients_asset_side",  curve:  {  name:  "Sensitive_churning_clients_asset_side",  ccid:  1  },  min:  0.5,  max:  0.6,  wgt:  0.5  }  },  {  inputValues:  [  {  bank:  {  name:  "Team1"  },  inputValue:  0.6  },  {  bank:  {  name:  "Team2"  },  inputValue:  0.6  }  ],  chrMSettings:  {  name:  "Service_quality_index",  curve:  {  name:  "Service_quality_asset_side",  ccid:  3  },  min:  0.5,  max:  0.6,  wgt:  0.5  }  }  ],  multiple:  0.5,  hqlaMultiple:  0.5  },  {  bsPosition:  {  name:  "liability_bc_timedeposits",  category:  "TIME_DEPOSITS",  type:  "LIABILITY"  },  mktTplus1GrowthPercentage:  0.5,  atrFeatures:  [  {  inputValues:  [  {  bank:  {  name:  "Team1"  },  inputValue:  0.5  },  {  bank:  {  name:  "Team2"  },  inputValue:  0.5  }  ],  atrMSettings:  {  name:  "Attracting_clients_asset_side",  curve:  {  name:  "Attracting_clients_asset_side",  ccid:  7  },  wgt:  0.5  }  },  {  inputValues:  [  {  bank:  {  name:  "Team1"  },  inputValue:  0.5  },  {  bank:  {  name:  "Team2"  },  inputValue:  0.5  }  ],  atrMSettings:  {  name:  "GDP_growth_on_loans",  curve:  {  name:  "BIP_growth_on_loans",  ccid:  8  },  wgt:  0.5  }  }  ],  chrFeatures:  [  {  inputValues:  [  {  bank:  {  name:  "Team1"  },  inputValue:  0.6  },  {  bank:  {  name:  "Team2"  },  inputValue:  0.6  }  ],  chrMSettings:  {  name:  "Sensitive_churning_clients_asset_side",  curve:  {  name:  "Sensitive_churning_clients_asset_side",  ccid:  1  },  min:  0.5,  max:  0.6,  wgt:  0.5  }  },  {  inputValues:  [  {  bank:  {  name:  "Team1"  },  inputValue:  0.6  },  {  bank:  {  name:  "Team2"  },  inputValue:  0.6  }  ],  chrMSettings:  {  name:  "Service_quality_index",  curve:  {  name:  "Service_quality_asset_side",  ccid:  3  },  min:  0.5,  max:  0.6,  wgt:  0.5  }  }  ],  }  ]  }  }  ]  };

const result = { banks: data.periods[0].decisions.map(decision => {
    // Determine the set of position categories.
    const categories = [...new Set(decision.bSPositionDecisions.map(r => r.balanceSheetPosition.category))];
    // Use reduce to create the position structure.
    return categories.reduce((pos, category) => { 
        pos[category] = decision.bSPositionDecisions.filter(d => d.balanceSheetPosition.category === category).map(d => {
            const settings = data.periods[0].settings.bSPositionSettings.find(p => p.bsPosition.name === d.balanceSheetPosition.name);
            const atrFeatures = settings.atrFeatures.map(feature => {
                const inputValue = feature.inputValues.find(val => val.bank.name ===  decision.bank.name);
                return { name: feature.atrMSettings.name, value: [inputValue.inputValue, feature.atrMSettings.curve.ccid, feature.atrMSettings.wgt ]};
            });
            const chrFeatures = settings.chrFeatures.map(feature => {
                return { name: feature.chrMSettings.curve.name, value: [feature.chrMSettings.curve.ccid, feature.chrMSettings.min, feature.chrMSettings.wgt, feature.chrMSettings.max  ]};
            });
            const ret = { Previous_year_Value: d.positionValue, totalInputRate: d.totalInputRate, name: d.balanceSheetPosition.name,  category: d.balanceSheetPosition.category } ;
            const features = [...atrFeatures, ...chrFeatures];
            features.forEach(feature => {
                ret[feature.name] = feature.value;
            });
            return ret;
        });
        return pos;
    }, { name: decision.bank.name } );
})};

console.log("Result:", result);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...