Javascript Node.js获取文件名,按整数值和返回的文件名进行排序,返回的文件名中包含最大值

Javascript Node.js获取文件名,按整数值和返回的文件名进行排序,返回的文件名中包含最大值,javascript,node.js,Javascript,Node.js,我正在使用该文件夹将用户变量存储在特定文件中。我想根据这些文件中写入的当前级别创建一个排名系统。所以我需要循环并读取所有文件,并分配等级 文件夹中的级别文件如下所示:192.168.0.2_Level.lvl、192.168.0.1_Level.lvl,它们包含一个整数值 我如何打开它们中的每一个并分配一个等级?具有最高级别的用户应为排名1的用户 我发现user.js代码的第一部分是: this.IP = socket.remoteAddress; this.level = fs.readFil

我正在使用该文件夹将用户变量存储在特定文件中。我想根据这些文件中写入的当前级别创建一个排名系统。所以我需要循环并读取所有文件,并分配等级

文件夹中的级别文件如下所示:192.168.0.2_Level.lvl、192.168.0.1_Level.lvl,它们包含一个整数值

我如何打开它们中的每一个并分配一个等级?具有最高级别的用户应为排名1的用户

我发现user.js代码的第一部分是:

this.IP = socket.remoteAddress;
this.level = fs.readFileSync(dbFolder + this.IP + "_level.lvl", "utf8");
var path = require('path');
var EXTENSION = '.lvl';
var files = fs.readdirSync(dbFolder);
var targetFiles = files.filter(function(file) {
    return path.extname(file) === EXTENSION;
});
console.log('Level files: ' + targetFiles);
在user.js的末尾,我需要设置一个排名:

this.rank = ????

我仍然错过了代码的中间部分。如果可能,请推荐完整的解决方案。

我建议您将级别存储在一个
levels.json
文件中,而不是多个文件中,并将用户排名存储在如下对象中:

{
  "users": {
    "192.168.0.2": 5, // or in an object, like this: { rank: 5 }
    "192.168.0.1": 8
  }
}
这样,您只需要获取一个文件,它将以与当前解决方案相同的方式进行扩展

加载数据后,只需执行常规的
.sort()


您可以通过使用承诺来改进加载代码。有许多方法可以将功能添加到node,但最新的版本将其内置到
fs

const fsp = require('fs').promises;
const path = './lvl';

async function readFiles() {

  // Read the file list
  const files = await fsp.readdir(path);

  // `map` over the file list and return a promise to read each one
  const promises = files.map(file => fsp.readFile(`${path}/${file}`, 'utf8'));

  // `Promise.all` waits for the promises to be resolved
  return Promise.all(promises);
};

(async () => {

  // Get your data
  const data = await readFiles();

  // `map` over the data again and assign ranks
  // depending on the value in the file
  const ranks = data.map(lvl => {
    if (lvl > 100) return { rank: 1, lvl };
    if (lvl > 50) return { rank: 2, lvl };
    if (lvl > 20) return { rank: 3, lvl };
    return { rank: 0, lvl };
  });

  // Assuming the files have content like 67, 50, 102,
  // you'll end up with an array of objects that looks
  // something like this:
  // [
  //   { rank: 2, lvl: '67' },
  //   { rank: 3, lvl: '50' },
  //   { rank: 1, lvl: '102' }
  // ]
  console.log(ranks);

})();
const fsp = require('fs').promises;
const path = './lvl';

async function readFiles() {

  // Read the file list
  const files = await fsp.readdir(path);

  // `map` over the file list and return a promise to read each one
  const promises = files.map(file => fsp.readFile(`${path}/${file}`, 'utf8'));

  // `Promise.all` waits for the promises to be resolved
  return Promise.all(promises);
};

(async () => {

  // Get your data
  const data = await readFiles();

  // `map` over the data again and assign ranks
  // depending on the value in the file
  const ranks = data.map(lvl => {
    if (lvl > 100) return { rank: 1, lvl };
    if (lvl > 50) return { rank: 2, lvl };
    if (lvl > 20) return { rank: 3, lvl };
    return { rank: 0, lvl };
  });

  // Assuming the files have content like 67, 50, 102,
  // you'll end up with an array of objects that looks
  // something like this:
  // [
  //   { rank: 2, lvl: '67' },
  //   { rank: 3, lvl: '50' },
  //   { rank: 1, lvl: '102' }
  // ]
  console.log(ranks);

})();