Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 当数字相同时,获取包含第二大数字的文件名_Node.js - Fatal编程技术网

Node.js 当数字相同时,获取包含第二大数字的文件名

Node.js 当数字相同时,获取包含第二大数字的文件名,node.js,Node.js,我试图得到第二大数字的文件名,它总是以最大的数字输出文件。我做错了什么? 示例:我有3个文件名,其中存储了数字,如:6、6或1,它总是输出第一个文件名,其中包含数字6。我需要输出第二个数字为6的文件名 const getSecondFileName = (pathToFolder) => { const files = fs.readdirSync(pathToFolder); const content = files.map(f => +fs.readFi

我试图得到第二大数字的文件名,它总是以最大的数字输出文件。我做错了什么? 示例:我有3个文件名,其中存储了数字,如:6、6或1,它总是输出第一个文件名,其中包含数字6。我需要输出第二个数字为6的文件名

const getSecondFileName = (pathToFolder) => {
      const files = fs.readdirSync(pathToFolder);
      const content = files.map(f => +fs.readFileSync(`${pathToFolder}/${f}`, 'UTF-8'));
      const arrayCopy = [...content];
      const secondLargestNum = arrayCopy.sort()[arrayCopy.length - 2]
      const secondFileWithLargestInteger = files[content.indexOf(secondLargestNum)];

      return secondFileWithLargestInteger;        
    }

谢谢您的回答。

由于indexOf返回第一个匹配项,并且您的firstLargestNum和secondLargestNum是相同的(这是一个特例),所以让我们分开处理

 const getSecondFileName = (pathToFolder) => {
      let secondFileWithLargestInteger;
      const files = fs.readdirSync(pathToFolder);
      const arrLength = files.length;
      const content = files.map(f => +fs.readFileSync(`${pathToFolder}/${f}`, 'UTF-8'));
      const arrayCopy = [...content];
      const sortedArray = arrayCopy.sort(function(a, b){
        return a - b;
      });;
      const firstLargestNum = sortedArray[arrLength- 1];
      const secondLargestNum = sortedArray[arrLength- 2];
      if (firstLargestNum === secondLargestNum) {
        const indexofFirst = content.indexOf(secondLargestNum);
        // const indexofFirst = content.indexOf(firstLargestNum); same 
        secondFileWithLargestInteger = files[content.indexOf(secondLargestNum, (indexofFirst + 1))]
      } else {
        secondFileWithLargestInteger = files[content.indexOf(secondLargestNum)];
      }
      return secondFileWithLargestInteger;        
    }
我在这里使用与您不同的方法进行排序,因为文件中的元素是字符串

var numbers = ['1', '5', '12', '3', '7', '15', '9']; 
// Sorting the numbers array simply using the sort method
numbers.sort(); // Sorts numbers array
alert(numbers); // Outputs: 1,12,15,3,5,7,9

Sort方法按字母顺序对数组元素进行排序

使用Sort时,请注意Sort将数组元素转换为字符串const array1=[6,40,7,21];array1.sort();=======>输出将是数组[21,40,6,7],在这里,第二个大num文件和第一个大num文件包含相同的值,您以特定的情况结束,您应该分别处理。因此,您建议做什么?我建议下面给出一个答案,让我知道,如果你想要更多的澄清尼斯后,但它总是输入随机文件,如果我添加文件或删除文件,它总是不同的刚刚发现,如果它是两位数的数字,使它疯狂,即使我的代码你知道为什么吗?有两位数,如果你使用上述建议排序,你会安全看到这个链接我更新代码,你的权利,它不起作用,让我们现在试一试请Ethanks明天再看:)在你的代码中,它没有实现排序的事情?