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

嵌套对象数组映射:按储蓄额及支票额排序客户数据

实现maxSavingsData函数

现有数据结构

const data = [
  {
    name: 'chase',
    advisors: [
      {
        name: 'mark',
        clients: [
          { name: 'carol', savings: 500, checking: 600 },
          { name: 'toby', savings: 500, checking: 300 },
          { name: 'nich' }
        ]
      },
      {
        name: 'holly',
        clients: [
          { name: 'john', savings: 900 },
          { name: 'jim', checking: 200 },
          { name: 'bruce', checking: 200 },
          { name: 'sarah', savings: 500, checking: 300 }
        ]
      }
    ]
  },
  {
    name: 'citiBank',
    advisors: [
      {
        name: 'cindy',
        clients: [ { name: 'casey', savings: 500, checking: 200 } ]
      },
      { name: 'bob', clients: null }
    ]
  },
  { name: 'hsbc', advisors: null }
];

需求说明

需要实现maxSavingsData函数,完成以下操作:

  • 提取所有包含savings或checking字段的客户数据
  • 为每个符合条件的客户添加firm(所属机构名称)和advisor(所属顾问名称)字段
  • 将最终客户数组按规则排序:优先按savings从高到低排序;若savings值相同,则按checking从高到低排序

预期输出示例:

[
  { name: 'john', savings: 900, firm:'chase',advisor:'holly' },
  { name: 'carol', savings: 500, checking: 600, firm: 'chase', advisor: 'mark' },
  { name: 'sarah', savings: 500, checking: 300, advisor:'holly',firm:'chase' },
  { name: 'toby', savings: 500, checking: 300, firm:'chase',advisor:'mark' },
  { name: 'casey', savings: 500, checking: 200,firm:'citiBank',advisor:'cindy' },
  { name: 'jim', checking: 200, firm:'chase',advisor:'holly' },
  { name: 'bruce', checking: 200, firm:'chase',advisor:'holly' }
]

待完善函数

const maxSavingsData = ()=>{
  const client = [];
  console.log(client);
}
maxSavingsData(data);

解决方案

以下是完整的函数实现:

const maxSavingsData = (data) => {
  const clients = [];
  
  // 遍历每家机构
  data.forEach(firm => {
    // 跳过无顾问的机构
    if (!firm.advisors) return;
    
    // 遍历每个顾问
    firm.advisors.forEach(advisor => {
      // 跳过无客户的顾问
      if (!advisor.clients) return;
      
      // 遍历每个客户
      advisor.clients.forEach(client => {
        // 筛选包含savings或checking的客户
        if ('savings' in client || 'checking' in client) {
          // 复制客户数据并添加firm和advisor字段
          clients.push({
            ...client,
            firm: firm.name,
            advisor: advisor.name
          });
        }
      });
    });
  });
  
  // 按规则排序:先按savings降序(无savings视为0),再按checking降序(无checking视为0)
  clients.sort((a, b) => {
    const savingsA = a.savings || 0;
    const savingsB = b.savings || 0;
    if (savingsB !== savingsA) {
      return savingsB - savingsA;
    }
    const checkingA = a.checking || 0;
    const checkingB = b.checking || 0;
    return checkingB - checkingA;
  });
  
  return clients;
}

// 调用函数并打印结果
console.log(maxSavingsData(data));

代码说明

  1. 遍历与筛选:通过多层遍历(机构→顾问→客户),筛选出包含savings或checking字段的客户,同时为其添加所属机构和顾问信息。
  2. 排序逻辑:排序时,将无savings或checking的字段视为0,先比较savings降序,若相同则比较checking降序。
  3. 空值处理:对advisors或clients为null的情况做跳过处理,避免报错。

内容的提问来源于stack exchange,提问作者Sam Mukherjee

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 14:15:35