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

基于相邻相同type值生成唯一对象数组列表

问题

给定一个包含重复type值的对象数组,需要按相邻相同type值分组生成新的数组列表:

  • 若当前对象与下一个相邻对象type相同,则归为同一组
  • 若type相同但不相邻,则单独组成新组

输入数组:

[ 
  { "object": 1, "start": "2018-07-08", "end": "2018-12-31", "type": "A", "user": "ABCD" }, 
  { "object": 2, "start": "2018-12-31", "end": "2019-06-26", "type": "A", "user": "ABCD" }, 
  { "object": 3, "start": "2019-06-26", "end": "2019-12-31", "type": "B", "user": "PQRS" }, 
  { "object": 4, "start": "2019-12-31", "end": "2020-06-31", "type": "B", "user": "PQRS" }, 
  { "object": 5, "start": "2021-12-31", "end": "2022-12-31", "type": "A", "user": "ABCD" }, 
  { "object": 6, "start": "2012-12-31", "end": "2022-06-26", "type": "A", "user": "ABCD" }, 
  { "object": 7, "start": "2022-06-26", "end": "2022-12-31", "type": "A", "user": "ABCD" }, 
  { "object": 8, "start": "2022-12-31", "end": "2023-06-26", "type": "A", "user": "ABCD" } 
]

预期输出:

[
   {
      "type":"A",
      "values":[
         {
            "object":1,
            "start":"2018-07-08",
            "end":"2018-12-31",
            "type":"A",
            "user":"ABCD"
         },
         {
            "object":2,
            "start":"2018-12-31",
            "end":"2019-06-26",
            "type":"A",
            "user":"ABCD"
         }
      ]
   },
   {
      "type":"B",
      "values":[
         {
            "object":3,
            "start":"2019-06-26",
            "end":"2019-12-31",
            "type":"B",
            "user":"PQRS"
         },
         {
            "object":4,
            "start":"2019-12-31",
            "end":"2020-06-31",
            "type":"B",
            "user":"PQRS"
         }
      ]
   },
   {
      "type":"A",
      "values":[
         {
            "object":5,
            "start":"2021-12-31",
            "end":"2022-12-31",
            "type":"A",
            "user":"ABCD"
         },
         {
            "object":6,
            "start":"2012-12-31",
            "end":"2022-06-26",
            "type":"A",
            "user":"ABCD"
         },
         {
            "object":7,
            "start":"2022-06-26",
            "end":"2022-12-31",
            "type":"A",
            "user":"ABCD"
         },
         {
            "object":8,
            "start":"2022-12-31",
            "end":"2023-06-26",
            "type":"A",
            "user":"ABCD"
         }
      ]
   }
]
解决方案

通过遍历数组并维护当前分组的方式实现:

  • 初始化结果数组与当前分组
  • 遍历每个元素,若当前分组为空,则直接以当前元素创建初始分组
  • 若当前元素type与当前分组的type一致,将元素加入当前分组的values列表
  • 若type不一致,将当前分组存入结果数组,再以当前元素新建分组
  • 遍历结束后,把最后一个分组存入结果数组

代码实现:

const input = [ 
  { "object": 1, "start": "2018-07-08", "end": "2018-12-31", "type": "A", "user": "ABCD" }, 
  { "object": 2, "start": "2018-12-31", "end": "2019-06-26", "type": "A", "user": "ABCD" }, 
  { "object": 3, "start": "2019-06-26", "end": "2019-12-31", "type": "B", "user": "PQRS" }, 
  { "object": 4, "start": "2019-12-31", "end": "2020-06-31", "type": "B", "user": "PQRS" }, 
  { "object": 5, "start": "2021-12-31", "end": "2022-12-31", "type": "A", "user": "ABCD" }, 
  { "object": 6, "start": "2012-12-31", "end": "2022-06-26", "type": "A", "user": "ABCD" }, 
  { "object": 7, "start": "2022-06-26", "end": "2022-12-31", "type": "A", "user": "ABCD" }, 
  { "object": 8, "start": "2022-12-31", "end": "2023-06-26", "type": "A", "user": "ABCD" } 
];

const groupAdjacentByType = (arr) => {
  if (!arr.length) return [];
  
  const result = [];
  let currentGroup = { type: arr[0].type, values: [arr[0]] };
  
  for (let i = 1; i < arr.length; i++) {
    const item = arr[i];
    if (item.type === currentGroup.type) {
      currentGroup.values.push(item);
    } else {
      result.push(currentGroup);
      currentGroup = { type: item.type, values: [item] };
    }
  }
  
  result.push(currentGroup);
  return result;
};

const output = groupAdjacentByType(input);
console.log(JSON.stringify(output, null, 3));

内容的提问来源于Stack Exchange,提问作者Er. IT

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 20:44:51