简体   繁体   English

如何检查JavaScript数组中是否存在某个值,继续添加0.15直到它不存在,然后将该值添加到数组的末尾?

[英]How to check if a value exists in a JavaScript array, continue to add 0.15 until it does not exist, then add that value to the end of the array?

I have a number of text input elements that will contain the start time for a persons shift (Eg 9:00, 9:30, 10:00, etc). 我有许多文本输入元素,其中将包含人员转移的开始时间(例如9:00、9:30、10:00等)。

My code will go through these elements one by one and push them to an array. 我的代码将一一遍历这些元素并将它们推入数组。 If the value already exists in the array, I need to add 15 minutes (0.15) to the time and then check if it exists again. 如果该值已存在于数组中,则需要将时间增加15分钟(0.15),然后检查它是否再次存在。 This will continue until the value does not exist and then push it to the array. 这将继续直到该值不存在,然后将其推入数组。

For example: John - 9:00 Bill - 9:00 Julie - 9:30 Sam - 9:30 Tony - 9:30 例如:John-9:00 Bill-9:00 Julie-9:30 Sam-9:30 Tony-9:30

times = [] 时间= []

The first time is 9:00 which is not in times, so it will be added. 第一次是9:00,这不是时间,因此将被添加。 Times is now equal to [9]. 现在的时间等于[9]。 Next one is 9:00 which does exist, so I add 0.15, giving me 9.15 which does not exist, so now times = [9, 9.15]. 下一个是确实存在的9:00,所以我加上0.15,得出不存在的9.15,所以现在时间= [9,9.15]。 This continues for all of the elements. 对于所有元素,这继续进行。 If the decimal points equal .60 then I need to add 1 to the hour, so for Tony, the time added would be 10. 如果小数点等于.60,则我需要将小时加1,因此对于Tony,添加的时间将是10。

I know that I can just use an if statement to check this once, but how do I continue to add 15 and continue to check 2, 3, 4+ times? 我知道我可以只使用if语句检查一次,但是如何继续加15并继续检查2、3、4+次?

if( times.contains(value)) {
      times.push(value + 0.15)
 }

To do it this way I would need heaps of if statements. 为此,我需要使用if语句堆。 How can I repeatedly check and then end the loop as soon as it's added? 添加后,如何反复检查并结束循环?

You could take a Set and increment the value until it is not in the set, then map this value. 您可以选择一个Set并递增该值直到它不在该set中,然后映射该值。

This approach uses a decimal value of 15 minutes, which is a quarter 0.25 hour. 此方法使用15分钟的十进制值,即0.25小时的四分之一。

 var times = [9, 9, 9.5, 9.5, 9.5], adjusted = times.map((s => t => { while (s.has(t)) { t += 0.25; } s.add(t); return t; })(new Set)); console.log(adjusted); 

One solution I can think of is 我能想到的一种解决方案是

  1. Store all the data in times regardless of their repeated value. 无论重复值如何,都应及时存储所有数据。 eg [9:00,9:00,9:30,9:30,9:30]; 例如[9:00,9:00,9:30,9:30,9:30];
  2. Iterate data and if i'th data === i-1'th data increment by 15 min. 迭代数据,如果第i个数据===第i-1个数据递增15分钟。
times = Arrays.sort(times);
for(let i=1;i<times.length;i++){
  times[i] = times[i]===times[i-1] ? add15min(times[i]) : times[i];
}

Iterations -: 迭代-:

  1. i=1 (times[i] === times[i-1] so increment) -- [9:00,9:15,9:30,9:30,9:30]; i = 1(times [i] === times [i-1]如此递增)-[9:00,9:15,9:30,9:30,9:30];
  2. i=2 (times[i] !== times[i-1] so no operation) -- [9:00,9:15,9:30,9:30,9:30]; i = 2(times [i]!== times [i-1]所以没有操作)-[9:00,9:15,9:30,9:30,9:30];
  3. i=3 (times[i] === times[i-1] so increment) -- [9:00,9:15,9:30,9:45,9:30]; i = 3(times [i] === times [i-1]如此递增)-[9:00,9:15,9:30,9:45,9:30];
  4. i=4 (times[i] === times[i-1] so increment) -- [9:00,9:15,9:30,9:45,10:00]; i = 4(times [i] === times [i-1]如此递增)-[9:00,9:15,9:30,9:45,10:00];

    Final data set [9:00,9:15,9:30,9:45,10:00]; 最终数据集[9:00,9:15,9:30,9:45,10:00];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM