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

请求编写Jolt Spec:基于stopId生成stopSequence计数器

为同一运单下的不同站点生成递增的站点序列值

需求核心:同一shipment_id下,相同stopId对应相同的stopSequence,不同stopId按首次出现顺序分配递增的序号。

实现思路

  1. 遍历输入数组,收集当前运单下所有唯一的stopId,按首次出现的顺序存储
  2. 建立stopId到stopSequence的映射关系
  3. 再次遍历原数组,给每个元素添加对应的stopSequence字段

代码实现(JavaScript)

// 输入数据
const inputData = [
  {
    "shipment_id": "a60e3760-2f7f-41f3-ac8d-2edd424bcfe4",
    "stopId": "111"
  },
  {
    "shipment_id": "a60e3760-2f7f-41f3-ac8d-2edd424bcfe4",
    "stopId": "111"
  },
  {
    "shipment_id": "a60e3760-2f7f-41f3-ac8d-2edd424bcfe4",
    "stopId": "222"
  },
  {
    "shipment_id": "a60e3760-2f7f-41f3-ac8d-2edd424bcfe4",
    "stopId": "222"
  },
  {
    "shipment_id": "a60e3760-2f7f-41f3-ac8d-2edd424bcfe4",
    "stopId": "222"
  },
  {
    "shipment_id": "a60e3760-2f7f-41f3-ac8d-2edd424bcfe4",
    "stopId": "333"
  },
  {
    "shipment_id": "a60e3760-2f7f-41f3-ac8d-2edd424bcfe4",
    "stopId": "333"
  }
];

// 生成stopId到stopSequence的映射
const stopMap = new Map();
let sequenceCounter = 1;

// 先遍历一次收集唯一stopId并分配序号
inputData.forEach(item => {
  if (!stopMap.has(item.stopId)) {
    stopMap.set(item.stopId, sequenceCounter++);
  }
});

// 给每个元素添加stopSequence字段
const outputData = inputData.map(item => ({
  ...item,
  stopSequence: stopMap.get(item.stopId)
}));

console.log(JSON.stringify(outputData, null, 2));

输出结果

[
  {
    "shipment_id": "a60e3760-2f7f-41f3-ac8d-2edd424bcfe4",
    "stopId": "111",
    "stopSequence": 1
  },
  {
    "shipment_id": "a60e3760-2f7f-41f3-ac8d-2edd424bcfe4",
    "stopId": "111",
    "stopSequence": 1
  },
  {
    "shipment_id": "a60e3760-2f7f-41f3-ac8d-2edd424bcfe4",
    "stopId": "222",
    "stopSequence": 2
  },
  {
    "shipment_id": "a60e3760-2f7f-41f3-ac8d-2edd424bcfe4",
    "stopId": "222",
    "stopSequence": 2
  },
  {
    "shipment_id": "a60e3760-2f7f-41f3-ac8d-2edd424bcfe4",
    "stopId": "222",
    "stopSequence": 2
  },
  {
    "shipment_id": "a60e3760-2f7f-41f3-ac8d-2edd424bcfe4",
    "stopId": "333",
    "stopSequence": 3
  },
  {
    "shipment_id": "a60e3760-2f7f-41f3-ac8d-2edd424bcfe4",
    "stopId": "333",
    "stopSequence": 3
  }
]

扩展说明

如果输入包含多个不同的shipment_id,只需在外层增加按shipment_id分组的逻辑:

const processShipments = data => {
  // 按shipment_id分组
  const shipmentGroups = {};
  data.forEach(item => {
    if (!shipmentGroups[item.shipment_id]) {
      shipmentGroups[item.shipment_id] = [];
    }
    shipmentGroups[item.shipment_id].push(item);
  });

  // 处理每个分组
  const result = [];
  Object.values(shipmentGroups).forEach(group => {
    const stopMap = new Map();
    let counter = 1;
    group.forEach(item => {
      if (!stopMap.has(item.stopId)) {
        stopMap.set(item.stopId, counter++);
      }
    });
    result.push(...group.map(item => ({...item, stopSequence: stopMap.get(item.stopId)})));
  });

  return result;
};

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 22:33:21