javascript 使用嵌套数组查找min和min

zyfwsgd6  于 2022-12-21  发布在  Java
关注(0)|答案(3)|浏览(115)

我想找到嵌套数组中的最小值和最大值,但是嵌套数组包含字符串和数字
当使用一个只有数字的变量时,我可以找到最小值和最大值,但是当它与一个字符串混合时,我就找不到最小值和最大值了,比如:

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
wfauudbj

wfauudbj1#

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

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;
}
*/

for (let i=1; i<monthsNet.length; ++i) {
  const item = monthsNet[i];
  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)
aiazj4mn

aiazj4mn2#

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, min } = monthsNet.reduce(({ max, min }, c) => {
    if (max[1] < c[1]) max = c;
    if (min[1] > c[1]) min = c;
    return { max, min }
}, { max: monthsNet[0], min: monthsNet[0] })
max = `Greatest Increase in Profits: ${max[0]} $ ${max[1]}`;
min = `Greatest Decrease in Profits: ${min[0]} $ ${min[1]}`;

console.log(max);
console.log(min);
omqzjyyz

omqzjyyz3#

使用带有扩散运算符的数学对象

获取数组中最小值/最大值的一个简单方法是使用spread operatorMath.minMath.max函数,创建一个只包含数字的数组,然后返回数组,找到包含最小值/最大值的项,并获取整个项以访问相应的日期。

let arr =[
  [ '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 ]
  ];
 
 const nums = arr.map(a => a[1]);

  const numMax = Math.max(...nums);
  const rowMax = arr.find(a => a[1] == numMax);
  const numMin = Math.min(...nums);
  const rowMin = arr.find(a => a[1] == numMin);

console.log(`Greatest Increase in Profits: $ ${rowMax[0]} ${rowMax[1]}`);
console.log(`Greatest Increase in Profits: $ ${rowMin[0]} ${rowMin[1]}`);

相关问题