Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用forEach()循环数组以获得最大数_Javascript_Arrays_Loops_Foreach - Fatal编程技术网

Javascript 使用forEach()循环数组以获得最大数

Javascript 使用forEach()循环数组以获得最大数,javascript,arrays,loops,foreach,Javascript,Arrays,Loops,Foreach,我试图使用forEach Javascript方法提取最大数量的数组。这是我到目前为止所知道的,但我知道这是不正确的 var-arr=[2,3,4]; var最大值=0; arr.forEach(功能(要素){ 如果(最大

我试图使用forEach Javascript方法提取最大数量的数组。这是我到目前为止所知道的,但我知道这是不正确的

var-arr=[2,3,4];
var最大值=0;
arr.forEach(功能(要素){
如果(最大});如果您放置
console.log(最大)
在forEach循环外部,它将打印数组中最大的数字。

如果您将
控制台.log(最大)
在forEach循环外部,它将打印数组中的最大数字。

这将起作用

var arr = [2,3,4];
var largest = 0;

arr.forEach(function(elem){
  if(largest < elem) 
  largest = elem;
});

console.log(largest);
这会奏效的

var arr = [2,3,4];
var largest = 0;

arr.forEach(function(elem){
  if(largest < elem) 
  largest = elem;
});

console.log(largest);

为了覆盖数组中的负数,可以安全地将
最大值
指定为数组中的第一个元素

var-arr=[2,3,4];
var最大值=arr[0];
arr.forEach(功能(要素){
如果(最大控制台日志(最大)
要覆盖数组中的负数,可以安全地将
最大值
指定为数组中的第一个元素

var-arr=[2,3,4];
var最大值=arr[0];
arr.forEach(功能(要素){
如果(最大
var
a=[2,3,4,2,1,9],
最大值=0;
a、 forEach(函数(值){
log('当前数组元素值:'+value+'。是否大于最大值?',(最大值>值));
如果(最大值<值)最大值=值;
log('现在最大的值是:',最大);
});
log('此处更改了最大值,因为它没有在forEach方法的函数中重新定义:',max',。\n但它是在其上下文中定义的');
var
a=[2,3,4,2,1,9],
最大值=0;
a、 forEach(函数(值){
log('当前数组元素值:'+value+'。是否大于最大值?',(最大值>值));
如果(最大值<值)最大值=值;
log('现在最大的值是:',最大);
});
log('此处更改了最大值,因为它没有在forEach方法的函数中重新定义:',max',。\n但它是在其上下文中定义的');

您可以简单地使用扩展运算符:

var-arr=[2,3,4];
var最大值=数学最大值(…arr);

控制台日志(最大)您可以简单地使用扩展运算符:

var-arr=[2,3,4];
var最大值=数学最大值(…arr);
控制台日志(最大)
var
    a = [2,3,4,2,1,9],
    largest = 0;

a.forEach(function(value){
    console.log('current array element value: ' + value + '.  Is it larger than the largest?', (largest > value));
    if (largest < value) largest = value;
    console.log('largest value now is: ', largest);
});
console.log('largest is changed here, because it was not redefined inside the function of the forEach method: ', largest, '.\n But it was defined within its context.');
function greatest(array) {
    if(array == undefined){
        console.log('There are no elements in array.');
    }

    const max = array.reduce(function(first, next){
        return first > next ? first : next; 
    });

    console.log(`Maximum element is ${max}`);
}