如何获取最小和最大日期并按ID分组

时间:2018-11-20 08:27:30

标签: javascript

我有这个数据数组

[{"id":1, "start":"2018-10-10", "end":"2018-11-10"},
 {"id":1, "start":"2018-11-10", "end":"2018-12-10"},
 {"id":2, "start":"2018-11-22", "end":"2018-11-30"}]

我想开始使用minimum,最后使用maximum

我想要的输出是

{"id":1, "start":"2018-10-10", "end":"2018-12-10"},
{"id":2, "start":"2018-11-22", "end":"2018-11-30"}

我试图这样做:

data.sort((a,b) => a.start.toString().localeCompare(b.start))

4 个答案:

答案 0 :(得分:1)

您要执行的操作将需要一个自定义解决方案,在该解决方案中您合并两个条目。在下面的函数中,我首先对具有相同id的块进行了迭代,然后根据id对它进行了排序,并保留了逻辑上的日期表示的索引的最小值和最大值

function groupById(arr){
  arr.sort((a,b)=>a.id-b.id);
  let arrNew = [];
  let min=0, max=0, currentid = arr[0].id;
  for(i=1; i<arr.length+1;i++){
    if(!arr[i] || arr[i].id!=currentid){
      arrNew.push({id:currentid, start:arr[min].start, end: arr[max].end});
      min = i;max=i;currentid=(arr[i]||{}).id;
    }
    if(!arr[i]){
      break;
    }
    if(arr[i].start<arr[min].start){
      min = i;
    }
    if(arr[i].end>arr[max].end){
      max = i;
    }
  }
  return arrNew;
}

var result = groupById([
  {"id":1, "start":"2018-10-10", "end":"2018-11-10"},
  {"id":1, "start":"2018-11-10", "end":"2018-12-10"},
  {"id":2, "start":"2018-11-22", "end":"2018-11-30"}
]);

console.log(result);

答案 1 :(得分:0)

您需要使用日期比较对数组进行排序,还需要根据ID reduce对数组进行排序。

var data = [{
    "id": 1,
    "start": "2018-10-10",
    "end": "2018-11-10"
  },
  {
    "id": 1,
    "start": "2018-11-10",
    "end": "2018-12-10"
  },
  {
    "id": 2,
    "start": "2018-11-22",
    "end": "2018-11-30"
  }
];


let result = data.sort((a, b) => new Date(a.start) > new Date(b.start)).reduce(function(r, a) {
  if (!r[a.id] || r[a.id][0].id !== a.id) {
    r[a.id] = r[a.id] || [];
    r[a.id].push(a);
  }

  return r;
}, Object.create(null));

console.log(result)

答案 2 :(得分:0)

您似乎有很多事情需要分组和简化(如引用其他问题的评论所示)。因此,我将在此答案中提供一些常规方法。

MDN对于此处使用的所有方法(例如map,reduce和Object.value)都有出色的文档,我建议您看看那里可以更好地了解代码。

ES6语法解释得很好here

const data = [
  { id: 1, start: '2018-10-10', end: '2018-11-10' },
  { id: 1, start: '2018-11-10', end: '2018-12-10' },
  { id: 2, start: '2018-11-22', end: '2018-11-30' },
];

const groupBy = (arr, key) =>
  arr.reduce(
    (result, item) => (
      result[item[key]].push(item), result
    ),
    arr.reduce(
      (result, item) => ((result[item[key]] = []), result),
      {},
    ),
  );
const returnLowHigh = (comp) => (a, b) =>
  a.localeCompare(b) === comp ? a : b;
const lowest = returnLowHigh(-1);
const highest = returnLowHigh(1);
console.log(
  Object.values(groupBy(data, 'id')).map((items) =>
    items.reduce((result, { id, start, end }) => ({
      id,
      start: lowest(result.start, start),
      end: highest(result.end, end),
    })),
  ),
);

答案 3 :(得分:0)

您可以使用减少功能并检查开始和结束日期以及开始日期是否为

let old = [{
    "id": 1,
    "start": "2018-10-10",
    "end": "2018-11-10"
  },
  {
    "id": 1,
    "start": "2018-11-10",
    "end": "2018-12-10"
  },
  {
    "id": 2,
    "start": "2018-11-22",
    "end": "2018-11-30"
  }
];

let k = old.reduce(function(acc, curr) {
  let findId = acc.findIndex((item) => {
    return item.id === curr.id
  });

  if (findId === -1) {
    acc.push(curr)
  } else {
    let oldStartDate = createDate(acc[findId].start);
    let newStartDate = createDate(curr.start);
    let oldEndDate = createDate(acc[findId].end);
    let newEndDate = createDate(curr.end);


    if (newStartDate < oldStartDate) {

      acc[findId].start = curr.start
    }
    if (newEndDate > oldEndDate) {

      acc[findId].end = curr.end
    }

  }

  return acc;

}, []);

console.log(k);

function createDate(dte) {
  let dt = new Date(dte);
  return `${dt.getYear()}-${dt.getMonth()}-${dt.getDate()}`

}