【问题标题】:How to insert an array into a nested JavaScript Object如何将数组插入嵌套的 JavaScript 对象
【发布时间】:2020-03-25 02:27:18
【问题描述】:

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

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

这是我目前得到的输出。映射用于创建此对象

{ 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”和从数据对象的“设置”部分获取的数组创建的输出

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

        } ],}

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

【问题讨论】:

    标签: javascript arrays object


    【解决方案1】:

    我们可以使用 map, filter reduce 来做这件事,这有点棘手,但同样的原理也适用:

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

    【讨论】:

    • 还有一件事,我如何在 atrFeatures 数组中添加“inputValue” 我试过这样做,但它在数组中返回一个未定义的值 const atrFeatures = settings.atrFeatures.map(feature => { return { name: feature.atrMSettings.name, value: [ feature.InputValues.inputValue, feature.atrMSettings.curve.ccid, feature.atrMSettings.wgt ] }; }
    • 我去看看! :)
    • 我已经做出了改变,包括输入值,我认为这会做你想要的!
    • 我有最后一个问题,你介意我把stackoverflow上的问题的链接发给你吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多