最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Other ways of getting the highest array value - Stack Overflow

programmeradmin3浏览0评论

I'm relatively new to JavaScript and am trying to get my head around the ES6 syntax specifically with if statements. I can create simple ES6 functions like:

function test(a,c) {
return a+c;
}
[3,8,-4,3].reduce(test);

However, if I want to add an if statement, I cannot get the ES6 syntax to work - for example:

function maxValue(a,c) {
if (c >= a) { a == c }
}
[3,8,-4,3].reduce(maxValue);

I know I can use the Math method to achieve the result via:

var array = [267, 306, 108];
var largest = Math.max.apply(Math, array); // returns 306

But I want to know how to use the new ES6 syntax properly. I've tried to add if statements in the filter, reduce, map and forEach to achieve the highest value in the array and am having difficulties.

Any help, advice or links to beginner material would be much appreciated, thanks.

I'm relatively new to JavaScript and am trying to get my head around the ES6 syntax specifically with if statements. I can create simple ES6 functions like:

function test(a,c) {
return a+c;
}
[3,8,-4,3].reduce(test);

However, if I want to add an if statement, I cannot get the ES6 syntax to work - for example:

function maxValue(a,c) {
if (c >= a) { a == c }
}
[3,8,-4,3].reduce(maxValue);

I know I can use the Math method to achieve the result via:

var array = [267, 306, 108];
var largest = Math.max.apply(Math, array); // returns 306

But I want to know how to use the new ES6 syntax properly. I've tried to add if statements in the filter, reduce, map and forEach to achieve the highest value in the array and am having difficulties.

Any help, advice or links to beginner material would be much appreciated, thanks.

Share Improve this question asked Nov 5, 2018 at 10:35 user8758206user8758206 2,2017 gold badges27 silver badges75 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 5

You could use a conditional operator with a check for the greater number.

const max = (a, b) => a > b ? a : b;

console.log([3, 8, -4, 3].reduce(max));

This approach does not work, if the array has only one element. In this case, you could use a very low number as start value and check against the given values.

const max = (a, b) => a > b ? a : b;

console.log([3].reduce(max, -Infinity));

For a way, without iterating, you could use Math.max with spread syntax ..., which take the elements of the array as parameter for the calling function.

console.log(Math.max(...[3, 8, -4, 3]));

For your code to work, you need to make 2 changes

  • Just like the simple case, you need to make sure you return the value. Because with return the value returned by function will be undefined and hence, the incorrect calculation
  • == is used for parison, for assignment, you need to use =

function maxValue(a,c) {
    if (c >= a) { a = c }
    return a;
}
console.log([3,8,-4,3].reduce(maxValue));

In ES6 you can use destructuring syntax.
Math.max(...array) is ES6 equivalent of Math.max.apply(Math, array).

var array = [267, 306, 108];
var largest = Math.max(...array);
console.log(largest)

Another option - use the spread operator with Math.max. The spread operator allows us to expand the contents of an array to pass as individual arguments. For example:

Math.max(...[1, 10, 2]);

is equivalent to:

Math.max(1, 10, 2);

Here's a demo:

const myArr = [-5, 0, 267, 306, 108, 57, -2352];
console.log(Math.max(...myArr));

发布评论

评论列表(0)

  1. 暂无评论