擅长:python、mysql、java
<p>在节点.js在做<code>npm i split2 through2 -S</code>之后</p>
<pre><code>const fs = require('fs')
const split = require('split2')
const through = require('through2')
const words = {}
fs.createReadStream('words.txt')
.pipe(split())
.pipe(through(write, end))
function write (buf, enc, next) {
const word = buf.toString()
words[word] = ++words[word] || 1
next()
}
function end () {
Object.keys(words)
.sort((a, b) => words[b] - words[a])
.forEach(word => {
console.log(`${word}: ${words[word]} times`)
})
}
</code></pre>