[英]A function that finds the highest value of an array in javascript?
I want to make a function that determines which day of the week i spent more money and if two or more days have the highest amount, an array containing the days should be returned or if the input is null or an empty array, the function should return null. 我想创建一个函数来确定我花了更多钱的一周中哪一天,如果两天或更多天的金额最高,则应返回包含日期的数组,或者如果输入为null或空数组,则函数应该返回null。
But i'm very confused. 但我很困惑。 So far i've tried to write the function that sorts the array in descending order but it doesn't seem right: 到目前为止,我已经尝试编写按降序对数组进行排序的函数,但它似乎不正确:
highamount= [50, 100, 2];
highamount.sort(function(amount[0],amount[1])) {
return amount[1]-amount[0];
}
What is wrong? 怎么了? And what if i don't have a defined array before and just want to sort it when i call the function, what would be in the function then? 如果我之前没有定义的数组,并且只是想在调用函数时对它进行排序,那么该函数会是什么呢?
I'm new to Javascript and i need help. 我是Javascript的新手,我需要帮助。
highamount.sort(function(amount[0], amount[1]))
because your closing your comparator
function with an extra parenthesis. 此行不正确 highamount.sort(function(amount[0], amount[1]))
因为您使用额外的括号关闭comparator
函数。 highamount.sort(function(amount[0], amount[1])) ^
compare
function, you're accessing to an undefined object/array amount
. 你没有在compare
函数中声明params,你正在访问未定义的对象/数组amount
。 function(amount[0], amount[1]) ^ ^
amount
, this is similar to the previous problem. 比较函数的逻辑存在问题,因为您正在访问未定义的对象/数组amount
,这与前一个问题类似。 amount[1] - amount[0] ^ ^
sort
function correctly, it's missing the closing parenthesis. 你没有正确关闭sort
函数,它缺少右括号。 return amount[1] - amount[0]; }; ^
Imagine your code has a correct syntax: 想象一下,您的代码具有正确的语法:
var highamount = [50, 100, 2]; highamount.sort(function(a, b) { return a - b; }); console.log(highamount);
See? 看到? your logic it's Ok because the array highamount
was sorted. 你的逻辑很好,因为数组highamount
已经排序了。 Now you can get the max value executing: highamount[highamount.length - 1]
现在你可以得到最大值执行: highamount[highamount.length - 1]
Good, how we can improve your approach to find the max value from your array highamount
. 好,我们如何改进您的方法,从您的阵列highamount
找到最大值。
The most recent Javascript version provides good capabilities to accomplish your scenario: 最新的Javascript版本提供了完成场景的良好功能:
With that operator, you can literally spread your Array and pass its values as params to other functions, arrays, Etc. 使用该运算符,您可以逐字传播数组并将其值作为参数传递给其他函数,数组等。
So, you can do the following: 因此,您可以执行以下操作:
var highamount = [50, 100, 2]; var max = Math.max(...highamount); console.log(max);
See? 看到? Amazing how Javascript provides a readable way to accomplish your scenario. 令人惊讶的是Javascript如何提供一种可读的方式来完成您的方案
Now, you wrote: And what if I don't have a defined array before and just want to sort it when I call the function, what would be in the function then? 现在,你写道: 如果我以前没有一个已定义的数组,并且只是想在调用函数时对它进行排序,那么该函数会是什么呢?
Well, you can declare a function which receives an array to find the max value. 那么,你可以声明一个接收数组的函数来找到最大值。
Run this code snippet: 运行此代码段:
var highamount = [50, 100, 2]; var findMax = function(array) { return array === undefined || array === null || array.length === 0 ? null : Math.max(...array); } console.log(findMax(highamount)); console.log(findMax(null)); console.log(findMax(undefined)); console.log(findMax([]));
See? 看到? the function findMax
returns the max value according to the passed array. 函数findMax
根据传递的数组返回最大值。
You can use the ES2015 spread syntax and Math.max for a very short one-liner: 您可以使用ES2015扩展语法和Math.max进行非常短的单行程序:
const numbers = [1, 5, 74, 27, 2, 8, 442, 12];
const largest = Math.max(...numbers); // > 442
The spread syntax passes all members of numbers
as arguments to Math.max
. spread语法将所有numbers
成员作为参数传递给Math.max
。 Therefore, the following 2 statements are equivalent: 因此,以下两个陈述是等效的:
Math.max(...numbers);
Math.max(1, 5, 74, 27, 2, 8, 442, 12);
The title question is different from the question you are ask in the body of the post, that's why no one else is giving you the right answer. 标题问题与你在帖子正文中提出的问题不同,这就是没有其他人给你正确答案的原因。 You want an array of the indexes of days that have the highest value. 您需要具有最高值的天数索引数组。 And in either case, sorting is not necessary. 在任何一种情况下,都不需要排序。
If you want an array of indexes of the highest values, ( or an array of Days, as you say). 如果你想要一个最高值的索引数组(或者像你说的那样是一个Days数组)。 Then this is how I would do it: 那我就是这样做的:
if (highAmount.length === 0) {
return null;
}
let highest = highAmount[0];
let indexes = [0];
for (var i = 1; i < highAmount.length; i++) {
if (highest < highAmount[i]) {
highest = highAmount[i];
indexes = [i];
}else if(highest = highAmount[i]){
indexes.push(i);
}
}
return indexes;
If you just want the highest value, all you have to do is iterate through the array. 如果您只想获得最高值,那么您只需遍历数组即可。
if (highAmount.length === 0) {
return null;
}
let highest = highAmount[0];
for (var i = 1; i < highAmount.length; i++) {
if (highest < highAmount[i]) {
highest = highAmount[i];
}
}
return highest;
Or just use Math.max(...highAmount) 或者只使用Math.max(... highAmount)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.