我正在根据以下条件过滤一系列Vet交易:
一个小时的时间为00:00:00-00:59:59、01:00:00-01:59:59,等等。>
在降低复杂性的同时,我想提出一种遵循最佳实践的易于阅读的解决方案。这是数据(已经按时间排序):
const dogs = [
{ "dog":"ralph", "timestamp":"2/23/2020 03:04:57", "amount": 140.00 },
{ "dog":"toto", "timestamp":"2/23/2020 03:14:31", "amount": 130.00 },
{ "dog":"toto", "timestamp":"2/23/2020 03:15:10", "amount": 145.00 },
{ "dog":"sadie", "timestamp":"2/23/2020 03:15:53", "amount": 175.00 },
{ "dog":"ralph", "timestamp":"2/23/2020 04:05:44", "amount": 220.00 },
{ "dog":"sadie", "timestamp":"2/23/2020 05:34:41", "amount": 100.00 },
{ "dog":"ralph", "timestamp":"2/23/2020 05:39:11", "amount": 40.00 },
{ "dog":"toto", "timestamp":"2/23/2020 05:43:00", "amount": 240.00 },
{ "dog":"toto", "timestamp":"2/23/2020 05:59:58", "amount": 235.00 },
{ "dog":"ralph", "timestamp":"2/23/2020 06:11:52", "amount": 20.00 },
{ "dog":"toto", "timestamp":"2/23/2020 06:12:53", "amount": 90.00 },
{ "dog":"rex", "timestamp":"2/23/2020 06:12:53", "amount": 315.00 },
{ "dog":"max", "timestamp":"2/23/2020 06:12:53", "amount": 285.00 },
{ "dog":"ralph", "timestamp":"2/23/2020 06:13:14", "amount": 240.00 },
{ "dog":"ralph", "timestamp":"2/23/2020 07:05:21", "amount": 60.00 },
{ "dog":"ralph", "timestamp":"2/23/2020 08:42:50", "amount": 80.00 },
{ "dog":"ralph", "timestamp":"2/23/2020 09:07:53", "amount": 100.00 },
{ "dog":"ralph", "timestamp":"2/23/2020 10:07:35", "amount": 200.00 },
{ "dog":"ralph", "timestamp":"2/23/2020 11:04:20", "amount": 120.00 },
{ "dog":"bella", "timestamp":"2/23/2020 11:04:40", "amount": 160.00 },
{ "dog":"sadie", "timestamp":"2/23/2020 11:04:54", "amount": 160.00 },
{ "dog":"bella", "timestamp":"2/23/2020 11:34:33", "amount": 160.00 },
{ "dog":"bella", "timestamp":"2/23/2020 11:44:23", "amount": 160.00 },
{ "dog":"bella", "timestamp":"2/23/2020 11:48:43", "amount": 125.00 },
{ "dog":"bella", "timestamp":"2/23/2020 12:03:53", "amount": 80.00 },
{ "dog":"bella", "timestamp":"2/23/2020 12:04:03", "amount": 100.00 },
{ "dog":"bella", "timestamp":"2/23/2020 13:11:54", "amount": 125.00 },
{ "dog":"ralph", "timestamp":"2/23/2020 14:04:35", "amount": 160.00 },
{ "dog":"bella", "timestamp":"2/23/2020 14:21:10", "amount": 170.00 },
{ "dog":"bella", "timestamp":"2/23/2020 15:15:18", "amount": 140.00 },
{ "dog":"bella", "timestamp":"2/23/2020 16:15:20", "amount": 180.00 },
{ "dog":"ralph", "timestamp":"2/23/2020 17:49:55", "amount": 180.00 }
]
这是我的工作解决方案:
function lessThanTen(dogs) {
let count = {}
let results = [];
for(let i = 0; i<dogs.length; i++) {
count[dogs[i].dog] ? count[dogs[i].dog] +=1 : count[dogs[i].dog] = 1;
}
for(let i = 0; i<dogs.length; i++) {
if(!(count[dogs[i].dog] > 10)) {
results.push(dogs[i]);
}
}
return results;
}
function mostExpensive(dogs) {
let curHour, nextHour, prevAmount, curAmount, nextAmount, highIndex;
let results = [];
const filtered = lessThanTen(dogs);
filtered.forEach((click, index) => {
curHour = filtered[index].timestamp.split(" ")[1].substring(0,2);
curAmount = filtered[index].amount;
if(index > 0) {
prevAmount = filtered[index-1].amount;
}
if(index < filtered.length - 1) {
nextHour = filtered[index + 1].timestamp.split(" ")[1].substring(0,2);
nextAmount = filtered[index + 1].amount
}
if ((curHour === nextHour) && ((curAmount > prevAmount && curAmount > nextAmount) || (curAmount === nextAmount && !highIndex)) ) {
highIndex = index;
}
if (nextHour > curHour) {
results.push(filtered[highIndex ? highIndex : index]);
highIndex = null;
}
});
console.log(results);
}
mostExpensive(dogs);
我是否可以/应该将“如果一个小时内同一只狗进行一次以上的交易”分解成它自己的功能,以便于测试?
是否有一种好的方法可以清除forEach中的所有if语句?我在过滤器上刺了一下,减少了,但不幸的是,在比较之前和当前的数量和时间时迷失了。
对于少于十个的函数,我是否应该使用for循环以外的东西?最佳实践是什么?我想不出一种避免使用两个循环O(2n)的方法。建议?
通常,什么是实现这三个标准的最清晰,最实用的方法?
我正在根据以下标准过滤一系列的Vet交易:对于一个小时内的每笔交易,仅将最昂贵的交易放入结果中(交易为{dog,...
in
运算符检查狗是否在count
集合中),而是: