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

验证Payload中指定键值存在性并按日期、起止时间过滤JSON数据

问题需求
  • 验证Payload中指定键对应的值是否存在
  • 基于给定的输入JSON,根据指定日期、时间段(开始/结束时间)过滤数据,最终输出符合指定结构的JSON结果

输入JSON

[
  {
    "building": 222,
    "default": {
      "money": 0.22,
      "perHour": null
    },
    "appPricing": [
      {
        "pricing": {
          "money": 1.25,
          "perHour": null
        },
        "schedules": [
          {
            "daysOfWeek": [
              "FRIDAY",
              "MONDAY"
            ],
            "end": "21:59:00",
            "start": "08:00:00"
          },
          {
            "days": [
              "TUESDAY",
              "WEDNESDAY",
              "THURSDAY"
            ],
            "end": "15:59:00",
            "start": "11:00:00"
          }
        ]
      }
    ]
  }
]

期望输出JSON

{ 
  "money": 0.22, 
  "perMinute": null, 
  "perSession": null
} 

实现方案

1. 键值存在性验证

先确保核心字段存在,避免后续处理报错:

  • 必须检查default.money、default.perHour(输出结构的基础来源)
  • 若涉及调度匹配,需检查appPricing[*].schedules[*]下的start、end,以及daysOfWeek/days(至少存在一个星期字段)

2. 数据过滤逻辑

示例输出使用了默认定价,说明当指定日期不在任何调度覆盖的星期/时间段内时, fallback到默认配置:

  1. 解析目标日期,获取大写格式的星期名称(如MONDAY、FRIDAY)
  2. 遍历appPricing中的调度规则,匹配目标日期对应的星期列表,同时验证当前时间是否在调度的start和end之间
  3. 匹配到有效调度则使用对应pricing数据,无匹配项则使用default数据

3. 结构转换

将获取到的定价数据转换为目标结构:

  • 保留money字段值
  • 将原perHour字段重命名为perMinute
  • 新增perSession字段并设为null

示例代码(JavaScript)

function processPricing(input, targetDate) {
  // 键值存在性验证
  const buildingData = input[0];
  if (buildingData.default.money === undefined) {
    throw new Error("缺失必填字段:default.money");
  }

  if (buildingData.appPricing) {
    buildingData.appPricing.forEach(item => {
      item.schedules.forEach(schedule => {
        if (!schedule.start || !schedule.end) {
          throw new Error("调度规则缺失start/end时间段");
        }
        if (!schedule.daysOfWeek && !schedule.days) {
          throw new Error("调度规则缺失星期配置");
        }
      });
    });
  }

  // 匹配调度规则
  const targetDay = targetDate.toLocaleString('en-US', { weekday: 'uppercase' });
  let selectedPricing = buildingData.default;

  if (buildingData.appPricing) {
    for (const pricingItem of buildingData.appPricing) {
      for (const schedule of pricingItem.schedules) {
        const validDays = schedule.daysOfWeek || schedule.days;
        if (validDays.includes(targetDay)) {
          // 此处可扩展:添加当前时间是否在start-end之间的判断逻辑
          selectedPricing = pricingItem.pricing;
          break;
        }
      }
      if (selectedPricing !== buildingData.default) break;
    }
  }

  // 转换为目标结构
  return {
    money: selectedPricing.money,
    perMinute: selectedPricing.perHour,
    perSession: null
  };
}

// 示例调用:目标日期为周六(不在任何调度规则中,使用默认定价)
const inputData = [
  {
    "building": 222,
    "default": {
      "money": 0.22,
      "perHour": null
    },
    "appPricing": [
      {
        "pricing": {
          "money": 1.25,
          "perHour": null
        },
        "schedules": [
          {
            "daysOfWeek": ["FRIDAY", "MONDAY"],
            "end": "21:59:00",
            "start": "08:00:00"
          },
          {
            "days": ["TUESDAY", "WEDNESDAY", "THURSDAY"],
            "end": "15:59:00",
            "start": "11:00:00"
          }
        ]
      }
    ]
  }
];
const targetDate = new Date('2024-06-15'); // 周六
console.log(processPricing(inputData, targetDate));

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 21:43:24