检查对象中的日期顺序

时间:2017-10-11 20:29:48

标签: javascript arrays sorting

我正在尝试确保在对象中输入的日期顺序是按逻辑顺序排列的。这是我的代码:

function checkDates(pet) {
        const dates = [
            pet.birthDate,
            pet.saleDate,
            pet.acquisitionDate,
            pet.deathDate
        ].filter( (date) => {
            // filter out undefined items
            return date;
        });

        // list of dates in their chronological order
        const sortedDates = dates.slice(0).sort();

        const inOrder = dates.every( (date, i) => {
            // check to make sure entered date is the same as the chronological date
            return date === sortedDates[i];
        });

        if (!inOrder) {
            throw new ValidationError('The dates are in an illogical order');
        }
    }

问题是saleDate和acquisitionDate不需要按顺序排列(如日期数组中所定义) - 它们只需要超过birthDate且小于deathDate。不需要不同的日期,例如,通过我的样子传递的宠物对象:

const pet = {
  name: "Sam",
  birthDate: "2017-01-01",
  acquisitionDate: "2017-02-01",
  saleDate: "2017-03-01"
}

进一步澄清:如果存在,birthDate必须始终排在第一位,而死亡日期必须始终排在最后。销售和收购必须在出生和死亡日期之间(如果存在),否则,如果在收购之前出售,则无关紧要,反之亦然。

4 个答案:

答案 0 :(得分:1)

您走的是正确的道路,但不一定要求排序:

function checkDates(pet) {
    const dates = [
        pet.birthDate,
        pet.saleDate,
        pet.acquisitionDate,
        pet.deathDate
    ].filter(date => date);

    const inOrder =
        (pet.birthDate ? dates.every(date => date >= pet.birthDate) : true) &&
        (pet.deathDate ? dates.every(date => date <= pet.deathDate) : true)

    if (!inOrder) {
        throw new ValidationError('The dates are in an illogical order');
    }
}

答案 1 :(得分:1)

您可以迭代给定的数组,而不进行排序,因为所有日期都必须按顺序排列。

&#13;
&#13;
git merge <branch> -m message
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您说saleDateacquisitionDate的顺序并不重要,只需要高于birthDate且低于deathDate,在这种情况下,可以简化您的功能,只做这四项检查:

function checkDates(pet) {
  var birthDate = new Date(pet.birthDate);
  var saleDate = new Date(pet.saleDate);
  var acquisitionDate = new Date(pet.acquisitionDate);
  var deathDate = pet.deathDate ? new Date(pet.deathDate) : Infinity;

  var inOrder = (birthDate < saleDate) && (birthDate < acquisitionDate) && (saleDate < deathDate) && (acquisitionDate < deathDate);

  if (!inOrder) {
    throw new ValidationError('The dates are in an illogical order');
  }
}

不需要使用array,对于排序和循环操作,它真的没用。

<强>演示:

function checkDates(pet) {
  var birthDate = new Date(pet.birthDate);
  var saleDate = new Date(pet.saleDate);
  var acquisitionDate = new Date(pet.acquisitionDate);
  var deathDate = pet.deathDate ? new Date(pet.deathDate) : Infinity;

  var inOrder = (birthDate < saleDate) && (birthDate < acquisitionDate) && (saleDate < deathDate) && (acquisitionDate < deathDate);

  if (!inOrder) {
    throw new ValidationError('The dates are in an illogical order');
  }
}
console.log(checkDates({
      name: "Sam",
      birthDate: "2018-01-01",
      acquisitionDate: "2017-02-01",
      saleDate: "2017-03-01"
    }));

答案 3 :(得分:0)

假设deathDate是此处唯一的可选属性,但似乎可以通过两次检查完成:

  • birthDate小于acquisitionDate / saleDate
  • 的最低值
  • 如果deathDatedeathDate大于acquisitionDate / saleDate
  • 的最大值

这些方面的东西:

&#13;
&#13;
function checkDates(pet) {
  const inOrder = (
    pet.birthDate < Math.min.apply(null, [pet.acquisitionDate, pet.saleDate]) &&
    pet.deathDate ? pet.deathDate > Math.max.apply(null, [pet.acquisitionDate, pet.saleDate]) : true
  );

  if (!inOrder) {
    throw new ValidationError('The dates are in an illogical order')
  }

  return true;
}


const pet = {
  birthDate: "2017-01-01",
  acquisitionDate: "2017-02-01",
  saleDate: "2017-03-01"
};

console.log(checkDates(pet));
&#13;
&#13;
&#13;

如果其他属性是可选的,会稍微改变一些但不会太大。