# 如何在地图中找到值的总和和平均值？

2023-01-06 15:18:17

const problem2 = new Map();

problem2.set('Julie', 13);

problem2.set('Jojo', 10);

problem2.set('Polly', 10);

problem2.set('Jack', 10);

problem2.set('Bruce', 10);

let sum = "";

for (const value of problem2.values()){

sum +=  parseInt (value, 10) +"\n";

};

sum;

## 3 回答

TA贡献1578条经验 获得超11个赞

let sum = 0;

problem2.forEach(value => sum += value); // value is problem

let average = sum / problem2.size

TA贡献1553条经验 获得超2个赞

const problem2 = new Map();

problem2.set('Julie', 13);

problem2.set('Jojo', 10);

problem2.set('Polly', 10);

problem2.set('Jack', 10);

problem2.set('Bruce', 10);

let sum = 0;

problem2.forEach(value => sum += value);

console.log('Sum: ' + sum);

console.log('Average: ' + (sum / problem2.size));

TA贡献1579条经验 获得超7个赞

const problem2 = new Map();

problem2.set('Julie', 13);

problem2.set('Jojo', 10);

problem2.set('Polly', 10);

problem2.set('Jack', 10);

problem2.set('Bruce', 10);

let sum = 0;

for (const value of problem2.values()){

sum += value;

};

console.log('Sum:',sum);

console.log('Average:', sum / problem2.size);

• 3 回答
• 0 关注
• 12 浏览

0/150