问题是,如何将包含字符串的文本文件添加到我的js文件中,我想检查字符串中的重复单词并在JavaScript中保留计数,但我不知道如何将文本文件添加到我的js文件中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";
我假设您想从浏览器执行此操作,没有提及
nodejs
环境,所以我的答案将反映浏览器解决方案。
您可以使用
input[type=file]
元素访问任何文件,然后点击 File api,在那里您将找到 .text()
承诺返回文件内容。
File接口没有定义任何方法,但继承了Blob接口的方法。
浏览器解决方案: :
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));
}
});
所以你可以先打开文件并根据要求写入连接。 如果你想阅读之前的连接,那么你可以使用:
阅读
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.
您可以使用文件系统(又名 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)
})
数据变量是文本文件中的文本。 您可以操纵它或用它做任何事情。
function countRepeatedWords(sentence) {
let words = sentence.split(" ");
let wordMap = {};
words.forEach(word => {
wordMap[word] = (wordMap[word] || 0) + 1;
});
return wordMap;
}
let sentence = "Awesome Javascript coding woohoo";
console.log(countRepeatedWords(sentence));
...