我创建了以下地图:

(function getLakes() {
    let lakes = new Map ([['Caspian Sea', 560], ['Tarn Hows', 53], ['Crater Lake', 324], ['Lake Tanganyika', 803], ['Lake Vostok', 546],
    ['Lake Baikal', 897]]);
    let fathom = 1.829;

    console.log("The deepest lake is " +  Math.max(...lakes.values())*fathom);
    })();


除了记录值之外,我还想记录键,以便最终得到以下附加行:
“最深的湖是贝加尔湖。”

我不确定如何进行这项工作-有人知道吗?

谢谢!

最佳答案

展开地图并在条目(Array.find()对)上使用[key, value]。使用解构来获取湖泊的名称(键)。



const lakes = new Map([
  ['Caspian Sea', 560],
  ['Tarn Hows', 53],
  ['Crater Lake', 324],
  ['Lake Tanganyika', 803],
  ['Lake Vostok', 546],
  ['Lake Baikal', 897]
]);

const fathom = 1.829;
const max = Math.max(...lakes.values());
const [lake] = [...lakes].find(([, v]) => v === max);

console.log(`The deepest lake is ${max * fathom}`);
console.log(`The deepest lake is ${lake}`);

10-06 04:38