我怎样才能 connect/add 一个文本文件到我的 JS 脚本

How can I connect/add a text file to my JS script

问题是,如何将包含字符串的文本文件添加到我的 js 文件中,我想检查字符串中重复的单词并在 JavaScript 中进行计数,但我没有关于如何将文本文件添加到我的 js 脚本的想法。

我的JS脚本是这样的:

let words = "Awesome Javascript coding woohoo";

function countRepeatedWords(sentence) {
  let words = sentence.split(" ");
  let wordMap = {};

  for (let i = 0; i < words.length; i++) {
    let currentWordCount = wordMap[words[i]];
    let count = currentWordCount ? currentWordCount : 0;
    wordMap[words[i]] = count + 1;
  }
  return wordMap;
}

console.log(countRepeatedWords(words));

所以我想添加我的文本文件(名为 TextFile2.txt),其中包含:

"Awesome Javascript coding woohoo woohoohoho";

然后从我的 JS 脚本中打印出我的文本文件字符串,而不是打印出:

let words = "Awesome Javascript coding woohoo";

所以你可以先打开文件,按要求写connect。 如果你想阅读以前的连接,那么你可以使用:

阅读 str = fread(file,flength(file) ;

file = fopen("c:\MyFile.txt", 3);// opens the file for writing fwrite(file, str);// str is the content that is to be written into the file.

您可以使用文件系统 aka fs 读取文本文件。 读取文本文件看起来像这样。

名为“TextFile2.txt”的文本文件:

Awesome Javascript coding woohoo woohoohoho

JavaScript 文件将包含以下内容:

const fs = require('fs')

fs.readFile('TextFile2.txt', 'utf8', (err, data) => {
    if (err) {
        console.error(err)
        return
    }
    console.log(data)
})

数据变量是文本文件中的文本。 你可以操纵它或用它做任何事。

我假设您想从浏览器执行此操作,没有提及 nodejs 环境,因此我的回答将反映浏览器解决方案。

您可以访问带有 input[type=file] 元素的任何文件并点击 File api,在那里您会发现 .text() 对 return 的承诺文件内容。

The File interface doesn't define any methods, but inherits methods from the Blob interface.

浏览器解决方案:

var words = "";

function countRepeatedWords(sentence) {
  let words = sentence.split(" "); // i would change it to sentence.split(/(\s|\t)+/);
  let wordMap = {};

  for (let i = 0; i < words.length; i++) {
    let currentWordCount = wordMap[words[i]];
    let count = currentWordCount ? currentWordCount : 0;
    wordMap[words[i]] = count + 1;
  }
  return wordMap;
}

// This function is called when the input has a change
function fileContents(element) {

  var file = element.files[0];
  file.text().then(text => {
    words = text; // update words
    // run your function 
    console.log(countRepeatedWords(words));
  })
}
<html>

<body>
  <input type="file" name="readThis" id="readThis" onChange="fileContents(this)" />
</body>

</html>

Node.JS解法:

const {readFile, readFileSync} = require('fs');

let file = '/path/to/your/file';

let words = "";

function countRepeatedWords(sentence) {
  let words = sentence.split(" ");
  let wordMap = {};

  for (let i = 0; i < words.length; i++) {
    let currentWordCount = wordMap[words[i]];
    let count = currentWordCount ? currentWordCount : 0;
    wordMap[words[i]] = count + 1;
  }
  return wordMap;
}

// Synchronous example
words = readFileSync(file).toString(); // convert buffer to string
console.log('Synchronous',countRepeatedWords(words));

// Asynchronous example
readFile( file, 'utf8' , (err, data)=> {
  
  if( err ){
    console.log(err);
  }else{
     
    words = data; // update words
    
    console.log('Asynchronous',countRepeatedWords(words));
  }

});