You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将数组插入嵌套JavaScript对象?附数据与结构示例

Insert Settings Data into Nested JavaScript Object Structure Using For Loops

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

  1. Extract Dependencies: We first pull the positionSettings from your original data object—this holds all the feature details we need to inject.
  2. Traverse Banks: For each bank in your existing banks array, we loop through its individual LOMBARD_LOANS entries.
  3. Match Settings to Loans: Using find(), we locate the corresponding configuration entry for each loan by matching the name property.
  4. Inject ATR Features: For each ATR feature, we extract the ccid from the curve and the wgt (weight), then assign them as an array to the loan entry under the feature's name.
  5. Inject CHR Features: For CHR features, we extract ccid, min, wgt, and max to form the required array, handling any missing name properties in the source data gracefully.
  6. Adjust Order (Optional): We reverse the banks array 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:09:30