在Javascript

时间:2018-11-07 15:45:49

标签: javascript

我有一个像这样的数据结构,它代表了公司的不同季度:

var quarters = [
 { beginning: 11, end: 1 },
 { beginning: 2, end: 4 },
 { beginning: 5, end: 7 },
 { beginning: 8, end: 10 }
]

现在,给定一个数字,我们将其称为month。我需要获取该数组中哪个对象具有月份。例如,如果month为12,它应该给我第一个对象{ beginning: 11, end: 1 }

我做了这个功能:

function findQuarter(month) {
 return quarters.find(
    q => q.beginning <= month && q.end >= month + 1,
  );
}

但是,例如,如果月份为11,它将找不到任何季度,而应该找到第一个季度。它不知道end不一定必须更大。

该四分之一变量是可自定义的,因此可以不同,例如:

 var quarters = [
     { beginning: 1, end: 3 },
     { beginning: 4, end: 6 },
     { beginning: 7, end: 9 },
     { beginning: 10, end: 12 }
    ]

 var quarters = [
             { beginning: 12, end: 2 },
             { beginning: 3, end: 5 },
             { beginning: 6, end: 8 },
             { beginning: 9, end: 11 }
            ]

如果给定的月份为1,应该找到{ beginning: 12, end: 2 },但不会。

代表公司中最常见的季度类型。

5 个答案:

答案 0 :(得分:3)

您可以将余数取12,然后将值调整为零到最大插槽长度之间的固定间隔。

这种方法使用模式将开始的值移动到零

(value + 12 - beginning) % 12
         ^^                   keep the result positive
              ^^^^^^^^^       take zero instead of a beginning
                         ^^^^ prevent number greater than 12

,最后以调整为较小或相等。余数运算符使用零基值将其值保持在wa所需范围内,所有值也小于十二。

function findMonth(month) {
    return quarters.find(({ beginning, end }) =>
        (month + 12 - beginning) % 12 <= (end + 12 - beginning) % 12 
    );
}

var quarters = [
    { beginning: 8, end: 10 }, 
    { beginning: 11, end: 1 }, 
    { beginning: 2, end: 4 }, 
    { beginning: 5, end: 7 }
];

console.log(findMonth(1));
console.log(findMonth(2));
console.log(findMonth(3));
console.log(findMonth(4));
console.log(findMonth(5));
console.log(findMonth(6));
console.log(findMonth(7));
console.log(findMonth(8));
console.log(findMonth(9));
console.log(findMonth(10));
console.log(findMonth(11));
console.log(findMonth(12));
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

您的条件还不完整,我还使用filter()方法来实现以下目的:

function findQuarter(month) {
 return quarters.filter(
    q => {
      if (q.beginning <= month && q.end >= month) {
        return q
      } else {
        if (q.beginning > q.end && month > q.end && month > q.beginning) return q
      }
    }
  );
}

var quarters = [
   { beginning: 1, end: 3 },
   { beginning: 11, end: 1 },
   { beginning: 4, end: 6 },
   { beginning: 7, end: 9 },
   { beginning: 10, end: 12 }
]

console.log(findQuarter(1))
console.log(findQuarter(12))
console.log(findQuarter(9))

答案 2 :(得分:1)

尝试类似这样的方法。

launch.json

答案 3 :(得分:0)

@Benjamin Gruenbaum说了什么。像这样更新您的功能:

function findQuarter(month) {
 return quarters.find(
    let effectiveEnd = (q.end < q.beginning) ? q.end + 12 : q.end
    q => q.beginning <= month && effectiveEnd >= month + 1,
  );
}

答案 4 :(得分:0)

我建议您采用以下解决方案:

    // QUARTERS SOURCE
    var quarters = [
      { beginning: 11, end: 1 },
      { beginning: 2, end: 4 },
      { beginning: 5, end: 7 },
      { beginning: 8, end: 10 }
    ];

    // FUNCTION
    const findQuarter = (month) => quarters.find(q => q.beginning <= month && q.end < q.beginning ? q.end + 12 : q.end >= month)

    // TO TEST
    for(var i = 1; i < 12; i++) {
      console.log(`The month ${i} is in the quarter ${findQuarter(i).beginning} - ${findQuarter(i).end}`);
    }

结果如下:

    The month 1 is in the quarter 11 - 1
    The month 2 is in the quarter 2 - 4
    The month 3 is in the quarter 2 - 4
    The month 4 is in the quarter 2 - 4
    The month 5 is in the quarter 5 - 7
    The month 6 is in the quarter 5 - 7
    The month 7 is in the quarter 5 - 7
    The month 8 is in the quarter 8 - 10
    The month 9 is in the quarter 8 - 10
    The month 10 is in the quarter 8 - 10
    The month 11 is in the quarter 11 - 1

这就是您所需要的吗? :)