简体   繁体   English

在 JavaScript 中,我将如何通过数组为 go 创建一个变量,不仅要记住最大值,还要记住该值的 position

[英]In JavaScript how would I create a variable to go through an array and remember not only the highest value but also the position of that value

var array = [1,2,3,4,5];
for(i = 0; i < array.length; i ++){

}

This is how far I have understood it (not my actual code just an example) and how would I use this for loop to go through the array and then not only remember the highest value but also the position that it is in. What I want to do specifically in my code is create a point system that increases the values of different position numbers in the array, then sorts through them to find the highest value and depending on which position/variable in the array has the highest value it will do something different.这就是我对它的理解程度(不是我的实际代码只是一个示例)以及我将如何使用这个 for 循环通过数组到 go 然后不仅记住最高值而且记住它所在的 position。我想要什么在我的代码中具体要做的是创建一个点系统,增加数组中不同 position 数字的值,然后对它们进行排序以找到最大值,并根据数组中的哪个位置/变量具有最高值它会做一些事情不同的。 apologies for the grammar.为语法道歉。

You could write a function that keeps track of the index with the highest value, max , and update it whenever arr[i] is greater -您可以编写一个 function 来跟踪具有max的索引,并在arr[i]更大时更新它 -

 function goThroughAnArrayAndRememberNotOnlyTheHighestValueButAlsoThePositionOfThatValue(arr) { if (arr.length == 0) return false let max = 0 for (let i = 1; i < arr.length; i++) if (arr[i] > arr[max]) max = i return { highestValue: arr[max], positionOfThatValue: max } } const input1 = [ 1, 6, 4, 3, 9, 5, 8, 7 ] const input2 = [] console.log(goThroughAnArrayAndRememberNotOnlyTheHighestValueButAlsoThePositionOfThatValue(input1)) // { highestValue: 9, positionOfThatValue: 4 } console.log(goThroughAnArrayAndRememberNotOnlyTheHighestValueButAlsoThePositionOfThatValue(input2)) // false

simply use an array.reduce method只需使用array.reduce 方法

 var myArray = [0,1,2,12,3,4,5] const f_MaxPos = arr => arr.reduce((r,c,i)=> { if (.i || c > r.max) { r.max = c r,pos = i } return r }: { max,undefined: pos.-1 }) console.log( JSON.stringify( f_MaxPos( myArray ) ) )
 .as-console-wrapper {max-height: 100%;important:top:0}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript - 如何使用原始数组和变量创建数组以确定数组的值可以多少次 go 进入变量 - Javascript - How to create an array using a orignal array and a variable to determine how many time a the value of an array can go into the variable Javascript:查找具有最高和第二高值的变量 - Javascript: Find variable with highest and second highest value 如何使用 Javascript 迭代和检索具有最高 innerText 值的元素? - How would I iterate and retrieve the element with the highest innerText value using Javascript? javascript数组找到最高价值 - javascript array find highest value 如何确定javascript数组中值&gt; 0的最后一项的索引 - How would I determine the index of the last item in javascript array with a value > 0 使用 for in 遍历对象值并返回最高值 - 我可以像使用 for 循环一样使用它吗 - Using for in to iterate through objects values and return the highest value - Can I use it in the same way I would a for Loop 如何在数组中找到最大值的键≤特定值? - How can I find the key of the highest value in an array ≤ a specific value? 如何使用括号表示法中的单个变量访问深度值? - how would I go about accessing a deep value using a single variable in bracket notation? 我将如何使用javascript根据字符串的值获取特定对象? - How would i go about getting a specific object based on the value of a string with javascript? 如何使用 json 数组创建一个新的 object,只有名称-值对和 javascript? - How do I use a json array to create a new object with only name-value pairs with javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM