我无法找到一种在objectMap中循环对象的方法,以便给出它收集的果实的最高价值。我的问题是,如何使用{{1}获得最高值的循环}循环。我在下面添加了我的代码。我已经停止了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 ( ) {
}
}
答案 0 :(得分: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'
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)
答案 1 :(得分:0)
如果 if(csvData[i][j] == null){
csvData[i][j]= '';
}
的值为fruitList
,则可以使用Array#forEach
进行循环并检查。如果不取零,否则取值为propery并加一个。然后分配给该属性。
fruit
&#13;
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);
&#13;
答案 2 :(得分:0)
像这样:
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);
&#13;
答案 3 :(得分:0)
此代码将返回销售的水果数量最多以及这些水果的名称。
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 );