繁体   English   中英

使用嵌套数组查找最小值和最小值

[英]Finding min and min with nested array

我想在嵌套数组中找到值的最小值和最大值,但是嵌套数组有字符串和数字

当使用仅包含数字的变量时,我能够找到最小值和最大值,但当它与字符串混合时,我无法找到最小值和最大值,如下所示:

let monthsNet={
[ 'Jan-2010', 116771 ],   [ 'Feb-2010', -662642 ],  [ 'Mar-2010', -391430 ],
  [ 'Apr-2010', 379920 ],   [ 'May-2010', 212354 ],   [ 'Jun-2010', 510239 ],
  [ 'Jul-2010', -428211 ],  [ 'Aug-2010', -821271 ],  [ 'Sep-2010', 693918 ],
  [ 'Oct-2010', 416278 ],   [ 'Nov-2010', -974163 ],  [ 'Dec-2010', 860159 ],
  [ 'Jan-2011', -1115009 ], [ 'Feb-2011', 1033048 ],  [ 'Mar-2011', 95318 ],
  [ 'Apr-2011', -308093 ],  [ 'May-2011', 99052 ],    [ 'Jun-2011', -521393 ],
  [ 'Jul-2011', 605450 ],   [ 'Aug-2011', 231727 ],   [ 'Sep-2011', -65187 ],
  [ 'Oct-2011', -702716 ],  [ 'Nov-2011', 177975 ],   [ 'Dec-2011', -1065544 ]
]



这是我使用的代码,仅使用带有数字的单独变量

let max = total[0];
let min = total[0];
for(i=0; i < total.length;i++){
  if(total[i] >= max){
   max = total[i]
   }
  if(total[i] <= min){
  min = total[i]
  }
}
console.log("Greatest Increase in Profits: $" + max)
console.log("Greatest Decrease in Profits: $" + min)

返回



Greatest Increase in Profits: $ 860159
Greatest Increase in Profits: $-1115009

但希望它返回这个(相应月份的最大值和最小值

Greatest Increase in Profits: Dec-2010 $ 860159
Greatest Increase in Profits: Jan-2011 $-1115009

存储数组,而不仅仅是最小/最大中的数字。

 let monthsNet = [ [ 'Jan-2010', 116771 ], [ 'Feb-2010', -662642 ], [ 'Mar-2010', -391430 ], [ 'Apr-2010', 379920 ], [ 'May-2010', 212354 ], [ 'Jun-2010', 510239 ], [ 'Jul-2010', -428211 ], [ 'Aug-2010', -821271 ], [ 'Sep-2010', 693918 ], [ 'Oct-2010', 416278 ], [ 'Nov-2010', -974163 ], [ 'Dec-2010', 860159 ], [ 'Jan-2011', -1115009 ], [ 'Feb-2011', 1033048 ], [ 'Mar-2011', 95318 ], [ 'Apr-2011', -308093 ], [ 'May-2011', 99052 ], [ 'Jun-2011', -521393 ], [ 'Jul-2011', 605450 ], [ 'Aug-2011', 231727 ], [ 'Sep-2011', -65187 ], [ 'Oct-2011', -702716 ], [ 'Nov-2011', 177975 ], [ 'Dec-2011', -1065544 ] ] let max = monthsNet[0]; let min = monthsNet[0]; for (const item of monthsNet) { if (item[1] > max[1]) max = item; if (item[1] < min[1]) min = item; } console.log("Greatest Increase in Profits:", max) console.log("Greatest Decrease in Profits:", min)

 let monthsNet = [ ['Jan-2010', 116771], ['Feb-2010', -662642], ['Mar-2010', -391430], ['Apr-2010', 379920], ['May-2010', 212354], ['Jun-2010', 510239], ['Jul-2010', -428211], ['Aug-2010', -821271], ['Sep-2010', 693918], ['Oct-2010', 416278], ['Nov-2010', -974163], ['Dec-2010', 860159], ['Jan-2011', -1115009], ['Feb-2011', 1033048], ['Mar-2011', 95318], ['Apr-2011', -308093], ['May-2011', 99052], ['Jun-2011', -521393], ['Jul-2011', 605450], ['Aug-2011', 231727], ['Sep-2011', -65187], ['Oct-2011', -702716], ['Nov-2011', 177975], ['Dec-2011', -1065544] ] let max = monthsNet[0]; let min = monthsNet[0]; monthsNet.forEach(m => { if (max[1] < m[1]) max = m; if (min[1] > m[1]) min = m; }) max = `Greatest Increase in Profits: ${max[0]} $ ${max[1]}`; min = `Greatest Increase in Profits: ${min[0]} $ ${min[1]}`; console.log(max); console.log(min);

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM