Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.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返回数组中最高值的所有索引_Javascript - Fatal编程技术网

Javascript返回数组中最高值的所有索引

Javascript返回数组中最高值的所有索引,javascript,Javascript,我正在尝试获取“all”的索引数组中的最高值: [0,1,4,3,4] 因此需要返回[2,4] 更新:感谢大家的快速回复。在阅读了之前的一些评论后,我想到了以下路径: var array = [0,1,4,3,4]; var indexes = []; var highest = Math.max(...array); array.forEach(function(element, i) { if (element == highest) { indexes.push

我正在尝试获取“all”的索引数组中的最高值:

[0,1,4,3,4]
因此需要返回
[2,4]

更新:感谢大家的快速回复。在阅读了之前的一些评论后,我想到了以下路径:

var array = [0,1,4,3,4];
var indexes = [];
var highest = Math.max(...array);

array.forEach(function(element, i) {
    if (element == highest) {
        indexes.push(i);
    }
});
我知道这有点冗长,但对我来说是有道理的。我最好读一下“减少”

这是我的想法

  • 首先使用math.max找到最大值
  • 现在循环遍历数组,如果值与
    max
设arr=[0,1,4,3,4]
设max=Math.max(…arr)
让op=[]

对于(让i=0;i首先使用
reduce
查找最大值,然后查找索引:

var-arr=[0,1,4,3,4];
var max=资产减少((会计科目,当前)=>当前>会计科目?当前:会计科目);
var res=arr.reduce((acc,curr,idx)=>curr==max?[…acc,idx]:acc,[]);

console.log(res);
使用
Math.max
您可以获得最大元素。Post将映射到数组并获得该最大元素的索引。这种方法的复杂性是O(n)

const arr=[0,1,4,3,4];
const max=数学最大值(…arr);
常数res=[];
arr.forEach((项目,索引)=>项目===max?res.push(索引):空);

console.log(res);
这是一个相当简单的版本。一旦获得最大值,在列表上迭代测试每个值是否匹配,如果相等,则添加索引:

constallmax=(xs,max=Math.max(…xs))=>
reduce((all,x,i)=>x==max?[…all,i]:all,[])

log(allMax([0,1,4,3,4])
这里有一个稍微详细的解决方案,它只迭代一次。其思想是跟踪所看到的最高值并与之进行比较

let arr = [0,1,4,3,4];
let maxValue = arr[0];
let maxIndexes = [];

arr.forEach((val, index) => {
  if(maxValue === val){
    maxIndexes.push(index);
  }

  if(maxValue < val){
    maxValue = val;
    maxIndexes = [index];
  }
})

console.log(maxIndexes);
设arr=[0,1,4,3,4];
设maxValue=arr[0];
设maxIndexes=[];
arr.forEach((val,index)=>{
if(maxValue==val){
push(索引);
}
如果(最大值
只需找到最大值

var max_val = Math.max.apply(null, array)
然后使用reduce函数

var max_val_indexes = arr.reduce(function(arr, ele, i) {
    if(ele === max_val)
        arr.push(i);
    return arr;
}, []);

要达到预期效果,请使用以下选项

  • 使用map循环
  • 捕获最大索引值
  • 筛选出最大索引值
  • var arr=[0,1,4,3,4]
    常数maxIndex=arr
    .map((v,i,self)=>v==Math.max(…self)?i:-1)
    .filter(索引=>索引!=-1);
    
    console.log(maxIndex)
    没有理由找到最大值,然后在数组中再次循环。您只需在遍历数组一次时跟踪当前最大值:

    设arr=[0,1,4,3,4]
    让maxIndexes=arr.reduce((maxes,n,i,a)=>{
    设cur=a[maxes[0]]//当前最大值
    if(cur==未定义的| | n>cur)返回[i]
    返回值(cur==n)?maxes.concat(i):maxes
    }, [])
    
    console.log(maxIndexes)
    您可以获得两个数组,一个是重复的maxim,另一个是maxim编号的索引。请检查下面的代码,它可能会帮助您

    设a=[1,3,6,6]
    Math.max.apply(数学,a);
    设指数=[];
    设y=0;
    设maxValue=a.reduce(函数(值,obj){
    if(数学最大值应用(数学,a)==obj){
    指数推力(y);
    推送值(obj);
    }
    ++y;
    返回值;
    },[]);
    console.log(maxValue);
    
    console.log(index);
    到目前为止你的努力?我使用过这个,但我只从中得到一个值:arr.indexOf(Math.max(…arr));当两个最大的数字是3和4时,为什么它要呈现[2,4]?@KévinHuang这是数组中两个4的索引数。谢谢我的坏消息!arr=[-5]失败了