在通过for in循环计算最大值时遇到麻烦

Having Trouble figuring out the highest value through a for in loop

本文关键字:最大值 遇到 麻烦 计算 循环 for in      更新时间:2023-09-26

我很难想出一种方法来循环objectMap中的对象,以给我它收集的水果的最大值。我的问题是,我如何使用for in循环来获得最大值。我在下面附上了我的代码。在我停下来的地方,通过for in循环

var fruitString = 'Banana,Banana,Pear,Orange,Apple,Melon,Grape,Apple,Banana,Grape,Melon,Grape,Melon,Apple,Grape,Banana,Orange,Melon,Orange,Banana,Banana,Orange,Pear,Grape,Orange,Orange,Apple,Apple,Banana'
  var fruitList = fruitString.split(',')
  var fruitMap = {};
  function soldfruits(){ 
    for (var i = 0; i < fruitList.length; i++) {
        var currentFruit = fruitList[i]
    if (fruitMap[currentFruit] === undefined) {// cehck if fruit is not available
         fruitMap[currentFruit]= 0;
        }
        fruitMap[currentFruit] = fruitMap[currentFruit] +1
    }
    console.log(fruitMap);
  }
  soldfruits(fruitMap); // calling function for check.
  for (var fruits in fruitMap) {
    if ( ) {
       }
  }

试试这个:

var fruitString = 'Banana,Banana,Pear,Orange,Apple,Melon,Grape,Apple,Banana,Grape,Melon,Grape,Melon,Apple,Grape,Banana,Orange,Melon,Orange,Banana,Banana,Orange,Pear,Grape,Orange,Orange,Apple,Apple,Banana'
var fruitList = fruitString.split(',')
var fruitMap = {};
function soldfruits(){
for (var i = 0; i < fruitList.length; i++) {
    var currentFruit = fruitList[i]
if (fruitMap[currentFruit] === undefined) {
     fruitMap[currentFruit]= 0;
    }
    fruitMap[currentFruit] = fruitMap[currentFruit] +1
}
console.log(fruitMap);
return fruitMap
}

var fmap = soldfruits(fruitMap);
var high = 0
var high_name = ''
for(var x in fmap)
{
    if(fmap[x] > high)
    {
         high = fmap[x]
         high_name = x
    }
}
console.log(high + ' ' + high_name)

此代码将返回售出水果的最高数量以及这些水果的名称。

var mostSold = 0;
var mostFruits = [];
for (var fruits in fruitMap) {
  if (fruitMap[fruits] > mostSold) {
    // store the number of fruit sold
    mostSold = fruitMap[fruits];
    // initialise the list with the fruit name
    mostFruits = [fruits];
  }
  else if (fruitMap[fruits] == mostSold) {
    // add the fruit name to the list
    mostFruits.push(fruits);
  }
}
console.log( mostSold + ' : ' + mostFruits );

像这样:

var highest = {fruit:"", num:0}
for (var fruit in fruitMap) {
  var num = fruitMap[fruit];
  if (num > highest.num) { // if the current number of fruits is greater than the saved
    highest.fruit=fruit; // save the fruit
    highest.num=num;     // save the number
  }
}
console.log(highest);

或者使用对象排序对数组进行排序并弹出最后一个

var fruitString = 'Banana,Banana,Pear,Orange,Apple,Melon,Grape,Apple,Banana,Grape,Melon,Grape,Melon,Apple,Grape,Banana,Orange,Melon,Orange,Banana,Banana,Orange,Pear,Grape,Orange,Orange,Apple,Apple,Banana'
var fruitList = fruitString.split(',')
var fruitMap = {};
function soldfruits() {
  for (var i = 0; i < fruitList.length; i++) {
    var currentFruit = fruitList[i]
    if (fruitMap[currentFruit] === undefined) {
      fruitMap[currentFruit] = 0;
    }
    fruitMap[currentFruit] = fruitMap[currentFruit] + 1
  }
  console.log(fruitMap);
}
soldfruits(fruitMap);
var highest = {fruit:"", num:0}
for (var fruit in fruitMap) {
  var num = fruitMap[fruit];
  if (num > highest.num) {
    highest.fruit=fruit;
    highest.num=num;
  }
}
console.log(highest);

您可以循环Array#forEach并检查fruitList是否具有属性fruit的值。如果不取零,则取属性的值并加1。然后赋值给属性

var fruitString = 'Banana,Banana,Pear,Orange,Apple,Melon,Grape,Apple,Banana,Grape,Melon,Grape,Melon,Apple,Grape,Banana,Orange,Melon,Orange,Banana,Banana,Orange,Pear,Grape,Orange,Orange,Apple,Apple,Banana',
    fruitList = fruitString.split(','),
    fruitMap = {},
    highest = 0;
fruitList.forEach(function (fruit) {
    fruitMap[fruit] = (fruitMap[fruit] || 0) + 1;
    if (fruitMap[fruit] > highest) {
        highest = fruitMap[fruit];
    }
});
console.log(fruitMap);
console.log(highest);
.as-console-wrapper { max-height: 100% !important; top: 0; }