gpt4 book ai didi

javascript - 如何检查数组元素是否大于特定值

转载 作者:行者123 更新时间:2023-12-02 21:39:32 25 4
gpt4 key购买 nike

我有以下数组,我需要检查该数组是否有 AvgWT

  1. 大于 15000 且小于 20000 则返回 50
  2. 大于 10000 且小于 15000 则返回 100
  3. 大于 5000 且小于 10000,则返回 150

我尝试了以下代码。

var arry = [{"TimeSlot":"8:00 am","AssistedC":0,"Abandons":0,"AvgWT":12152},{"TimeSlot":"8:15 am","AssistedC":0,"Abandons":0,"AvgWT":13942},{"TimeSlot":"8:30 am","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"8:45 am","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"9:00 am","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"9:15 am","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"6:30 pm","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"6:45 pm","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"7:00 pm","AssistedC":0,"Abandons":0,"AvgWT":0}];

function isArrayValueGreater(arry) {
// var first = array[0];
var first = arry[0]["AvgWaitTime"];
return arry.every(function(element) {
if(element["AvgWaitTime"]> 15000 && element["AvgWaitTime"]< 20000){
return 50;
}else if(element["AvgWaitTime"]> 10000 && element["AvgWaitTime"]<15000){
return 100;
}
});
}

isValuesSameG = isArrayValueGreater(arry);

但它无法正常工作,我在这里做错了什么?

最佳答案

如果我理解正确,你可以使用 .map() 方法。它将返回具有相同长度的新数组,包括匹配检查和结果。这是代码

const arry = [{"TimeSlot":"8:00 am","AssistedC":0,"Abandons":0,"AvgWT":12152},{"TimeSlot":"8:15 am","AssistedC":0,"Abandons":0,"AvgWT":13942},{"TimeSlot":"8:30 am","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"8:45 am","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"9:00 am","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"9:15 am","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"6:30 pm","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"6:45 pm","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"7:00 pm","AssistedC":0,"Abandons":0,"AvgWT":0}];

function checkIfMatchCondition({ AvgWT: value }) {
if(value < 20000 && value > 15000)
return 50
else if(value < 15000 && value > 10000)
return 100
else if(value < 10000 && value > 5000)
return 150
return 0
}

const res = arry.map(checkIfMatchCondition);
console.log(res)

可以通过包含索引或具有附加属性的整个对象来改进它,但原始帖子中没有足够的详细信息。

关于javascript - 如何检查数组元素是否大于特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60399899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com