如何将数组插入嵌套JavaScript对象?附数据与结构示例
Problem Description
Given a raw data object containing bank decisions and configuration settings, we already have a mapped banks structure that includes each team's LOMBARD_LOANS entries and their totalInputRate. We need to inject specific feature arrays from data.settings.bSPositionSettings into each loan entry to achieve the target nested object structure, using for loops instead of mapping functions.
Solution Code
Here's a step-by-step implementation using for loops to insert the required settings data into your existing banks structure:
// Your original data object (as provided) 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 }, { 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.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 } } ], } ] } ] }; // Your existing mapped banks structure let 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' } ] } ]; // Extract position settings from data const positionSettings = data.settings.bSPositionSettings; // Loop through each bank for (const bank of banks) { // Loop through each loan entry in the bank's LOMBARD_LOANS for (const loan of bank.LOMBARD_LOANS) { // Find the matching position setting by loan name const matchedSetting = positionSettings.find(setting => setting.bsPosition.name === loan.name); if (matchedSetting) { // Process ATR features (Attracting_clients_asset_side, GDP_growth_on_loans) for (const atrFeature of matchedSetting.atrFeatures) { const { name, curve, wgt } = atrFeature.atrMSettings; loan[name] = [curve.ccid, wgt]; } // Process CHR features (Sensitive_churning_clients_asset_side, Service_quality_index) for (const chrFeature of matchedSetting.chrFeatures) { const { name, curve, min, max, wgt } = chrFeature.chrMSettings; // Handle cases where name might be missing in source data const featureName = name || 'Sensitive_churning_clients_asset_side'; loan[featureName] = [curve.ccid, min, wgt, max]; } } } } // Optional: Reverse banks order to match your target structure (Team2 first) banks = banks.reverse(); // Log the final structure console.log(JSON.stringify(banks, null, 2));
Explanation
- Extract Dependencies: We first pull the
positionSettingsfrom your originaldataobject—this holds all the feature details we need to inject. - Traverse Banks: For each bank in your existing
banksarray, we loop through its individualLOMBARD_LOANSentries. - Match Settings to Loans: Using
find(), we locate the corresponding configuration entry for each loan by matching thenameproperty. - Inject ATR Features: For each ATR feature, we extract the
ccidfrom the curve and thewgt(weight), then assign them as an array to the loan entry under the feature's name. - Inject CHR Features: For CHR features, we extract
ccid,min,wgt, andmaxto form the required array, handling any missingnameproperties in the source data gracefully. - Adjust Order (Optional): We reverse the
banksarray to match your target structure where Team2 appears first.
This implementation uses simple for loops to traverse and modify the nested object, ensuring you get the exact structure you're aiming for.
内容的提问来源于stack exchange,提问作者Clive Charles

