我有一个以空格分隔的 .txt 文件,其中包含重复项。我想删除受骗者,但发现这不是一件容易的事。
文件包含:orange orange apple apple pear
起初,我遇到了 txt 扩展名的错误。我更新了 main 以包含
const fs = require('fs');
require.extensions['.txt'] = function (module, filename) {
module.exports = fs.readFileSync(filename, 'utf8');
这有助于解决错误,之后我能够创建一个 const
。
const fruitList = require('../support/fruitList.txt');
但是,我仍然无法删除欺骗。我试过 neek
也没有用。
最佳答案
您可以使用集合来删除集合中的重复项。
let fruitList = ["orange", "orange", "apple", "apple", "pear"];
let fruitSet = new Set(fruitList); // {"orange", "apple", "pear"}
//convert back to array
const newArray = [...fruitSet];//["orange", "apple", "pear"]
关于javascript - 如何使用 ES6 和 Node Js 删除 txt 文件中的重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58925538/