如何将数组插入嵌套的JavaScript对象

如何将数组插入嵌套的JavaScript对象

本文介绍了如何将数组插入嵌套的JavaScript对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前使用的对象是这样的

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

这是我现在得到的输出。映射用于创建该对象

This is currently the output i am getting. Mapping was used to create this object

{ banks:
  [ { name: 'Team1',
      LOMBARD_LOANS:
       [ { totalInputRate: 1,
           name: 'asset_bc_lombard_a_onsight',
           category: 'LOMBARD_LOANS' },
         { totalInputRate: 2,
           name: 'asset_bc_lombard_a_lt1m',
           category: 'LOMBARD_LOANS' } ] },
    { name: 'Team2',
      LOMBARD_LOANS:
       [ { totalInputRate: 5,
           name: 'asset_bc_lombard_a_onsight',
           category: 'LOMBARD_LOANS' },
         { totalInputRate: 6,
           name: 'asset_bc_lombard_a_lt1m',
           category: 'LOMBARD_LOANS' } ] } ] }

这是我尝试使用 totalinputrate和 setting部分中的数组创建的输出数据对象的

This is the output i try to create with the "totalinputrate" and the arrays which are taken from the "setting" part of the data object

{ banks:
   [ { name: 'Team2',
       LOMBARD_LOANS:
        [ { Sensitive_churning_clients_asset_side: [ 1, 0.5, 0.5, 0.6 ],
            Service_quality_index: [ 3, 0.5, 0.5, 0.6 ],
            Attracting_clients_asset_side: [ 7, 0.3 ],
            GDP_growth_on_loans: [ 8, 0.5 ],
             totalInputRate: 1,
            name: 'asset_bc_lombard_a_onsight',
            category: 'LOMBARD_LOANS' },
          { Sensitive_churning_clients_asset_side: [ 1, 0.5, 0.5, 0.6 ],
            Service_quality_index: [ 3, 0.5, 0.5, 0.6 ],
            Attracting_clients_asset_side: [ 7, 0.5 ],
            GDP_growth_on_loans: [ 8, 0.5 ],
            totalInputRate: 2,
            name: 'asset_bc_lombard_a_lt1m',
            category: 'LOMBARD_LOANS' } ]

        },
     { name: 'Team1',
       LOMBARD_LOANS:
        [ { Sensitive_churning_clients_asset_side: [ 1, 0.5, 0.5, 0.6 ],
            Service_quality_index: [ 3, 0.5, 0.5, 0.6 ],
            Attracting_clients_asset_side: [ 7, 0.3 ],
            GDP_growth_on_loans: [ 8, 0.5 ],
            totalInputRate: 5,
            name: 'asset_bc_lombard_a_onsight',
            category: 'LOMBARD_LOANS' },
          { Sensitive_churning_clients_asset_side: [ 1, 0.5, 0.5, 0.6 ],
            Service_quality_index: [ 3, 0.5, 0.5, 0.6 ],
            Attracting_clients_asset_side: [ 7, 0.5 ],
            GDP_growth_on_loans: [ 8, 0.5 ],
            totalInputRate: 6,
            name: 'asset_bc_lombard_a_lt1m',
            category: 'LOMBARD_LOANS' } ],

        } ],}

我没有使用映射来创建此结构,而是使用了循环。如果您也想查看该代码,请告诉我。

I didn't use mapping to create this structure instead I used for loops. Please let me know if you want to see that code as well .

推荐答案

为此,我们可以使用map,filter reduce,虽然有些棘手,但适用相同的原理:

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

这篇关于如何将数组插入嵌套的JavaScript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:29