Node.js;按顺序列出 JSON 文件中的字符串

Node.js; list strings from JSON file in order

我有文件 users.json,其中包含如下数据:

[
  {
     "nick": "user123",
     "active24": 579
     ...
  }, {
     "nick": "nick",
     "active24": 259
     ...
  }
]

我想制作排行榜,根据“active24”(从大到小)显示前 10 名用户昵称(来自未知的用户最大值)。我可以使用数组来做到这一点,但问题是机器人无法使用 vk-io 发送它(原因并不重要,所以我在这里不做太多描述,但请不要将最终输入保存为数组,而是作为变量等)。我想要这样的输出:

Var_top1 = `${nick}: ${active24}`;
Var_top2 = `${nick}: ${active24}`;
Var_top3 = `${nick}: ${active24}`;
...
Var_top10 = `${nick}: ${active24}`;

P.S对不起我的英语不好

使用如下方法对数组进行排序 array.prototype.sort.

数组的所有成员排序后,您只需打印它们array[0].nick + ":" + array[0].active24

let jsonObject = [
    {
        "nick": "user123",
        "active24": 579
    },
    {
        "nick": "user234",
        "active24": 1
    },
    {
        "nick": "nick",
        "active24": 259
    }
];

// Orders the array
jsonObject.sort((firstElem, secondElem) => {
    if (firstElem.active24 > secondElem.active24) return 1;
    if (firstElem.active24 < secondElem.active24) return -1;
    return 0;
})

// Prints the entire ordered array 
console.log(JSON.stringify(jsonObject, null, 2));

// Prints the nick and the active24 value
for (let i = 0; i < jsonObject.length; i++) {
    console.log(jsonObject[i].nick + ":" + jsonObject[i].active24);
}

你想要这样的东西吗?

const data = [
  {
    nick: "user111",
    active24: 579
  },
  {
    nick: "nick222",
    active24: 0
  },
  {
    nick: "nick333",
    active24: 84
  },
  {
    nick: "nick444",
    active24: 9459
  }
];

  const sorted = data.sort((a, b) => b.active24 - a.active24);
  console.log(JSON.stringify(sorted)); 
  //[{"nick":"nick444","active24":9459},{"nick":"user111","active24":579},{"nick":"nick333","active24":84},{"nick":"nick222","active24":0}]
  const top1 = sorted[0];
  const top2 = sorted[1];
  const top3 = sorted[2];
  const top4 = sorted[3];