Here's the updated code that reads all the contents in subfiles, returns all integers in a single array and returns the sum of all the items in the array:
const fs = require('fs')
let output = (file) => {
let data = fs.readFileSync(file, 'utf8')
.split('\n')
.reduce((array, i) => {
if (i.match(/.txt$/)) {
let intArr = array.concat(output(i))
return intArr
} else if (i.match(/^\d+$/)) {
array.push(parseInt(i, 10));
}
return array;
}, [])
return data
}
console.log(output('a.txt')) // single array
const sum = output('a.txt')
console.log(sum.reduce((a, b) => a + b, 0)) // sum of array contents
I'm trying to get the result of the following code into a single array so I can get the sum of all the numbers. Consider that the contents of the file a.txt
are
1
b.txt
the contents of b.txt
are
2
c.txt
and the contents of c.txt
are
3
The code is as follows:
const fs = require('fs')
let output = (file) => {
let data = fs.readFileSync(file, 'utf8')
.split('\n')
.map((i) => {
return i
})
let subData = data
.filter(value => /.txt$/.test(value))
.map((i) => {
output(i)
return i
})
let intData = data
.filter(value => /^\d+$/.test(value))
.map((i) => {
return parseInt(i, 10)
})
console.log(intData)
}
output('a.txt')
This outputs:
[3]
[2]
[1]
Which would be the best way of getting the sum of these numbers?
You can modify your code by using the .reduce
method instead of .map
:
The
reduce()
method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.
const fs = require('fs')
let output = (file) => {
let data = fs.readFileSync(file, 'utf8')
.split('\n')
.reduce((sum, i) => {
if (i.match(/.txt$/)) {
sum += output(i);
} else if (i.match(/^\d+$/)) {
sum += parseInt(i, 10);
}
return sum;
}, 0)
}
If you want to save the integers in an array you could reduce it to an array instead:
const fs = require('fs')
let output = (file) => {
let data = fs.readFileSync(file, 'utf8')
.split('\n')
.reduce((array, i) => {
if (i.match(/.txt$/)) {
array.concat(output(i));
} else if (i.match(/^\d+$/)) {
array.push(parseInt(i, 10));
}
return array;
}, [])
}
Then if you want to get the sum of the file a.txt
do:
const sum = output('a.txt').map((sum,i) => sum + i);
Below uses the promisified version of readFile, I've left it intentionally a bit verbose to demonstrate what's going on, you could obviously simplify/shorten that a bit.
However, it takes an array of files, filters out every number and sums all of them
const fs = require('fs');
const {promisify} = require('util');
const readFileAsync = promisify(fs.readFile);
function readAndSum(file) {
return readFileAsync(file, 'utf8')
.then(f => f.split('\n'))
.then(lines => {
return lines.map(line => {
let newFile = line.match(/\w+\.txt/);
if (newFile) {
return readAndSum(newFile[0]);
}
return Number(line);
})
})
.then(t => Promise.all(t.reduce((a, b) => a.concat(b), [])))
.then(numbers => numbers.reduce((a, b) => a + b, 0))
}
readAndSum('foo.txt').then(sum => console.log(sum));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.