变化:在 Javascript 中找到具有最高值的对象

Variation: Find the object with the highest value in Javascript

Finding the max value of an attribute in an array of objects 中有许多(很棒的)答案报告了数组中的最大值,但他们没有选择具有最高值的对象将是报告所需的结果。

我正在寻找搜索数组中最大值和 return 具有该值的对象的最佳方法。例如检查这个数组的预期结果:

    {
      "Intent": {
        "FileComplaint": 0.000,
        "UnsubscribeMe": 0.995,
        "TrackRefund": 0.001,
        "AskSwitchAccount": 0.00
      }

将是:“UnsubscribeMe”或“UnsubscribeMe”:0.995。

谁能帮忙?

编辑: 我发现了一个比我的问题表述得更好的问题,它有很好的答案: Getting key with the highest value from object

const obj={Intent:{FileComplaint:0,UndermineGovernment:0.45,UnsubscribeMe:.995,TrackRefund:.001,AskSwitchAccount:0}};

// Get the entries as a nested array of key/value pairs
const entries = Object.entries(obj.Intent);

// Sort the entries by value (index 1),
// and then pop off the last entry destructuring
// the key/value from that array in the process
const [key, value] = entries.sort((a, b) => a[1] > b[1]).pop();

// Log the resulting object
console.log({ [key]: value });