Currently, my task involves tallying the number of times a specific email has made contact with us. These contacts are stored in JSON files with the key being "email".
The challenge here is that there could be an infinite number of JSON files, so I am looking to consolidate them into a single object and then iterate through them to calculate the frequency of each email.
To clarify, I need to first read the JSON content, log it, process the message, and then convert that message into a count of logs per email used.
My initial approach might not be correct, but I believe I need to merge all the JSON files into one object that I can loop through and manipulate as required. However, I seem to be facing synchronization issues.
I am using fs to read in (I anticipate around 100 JSON files), looping through them with forEach, and trying to push each entry into an array. However, the array keeps coming back empty. I am certain it's something simple that I'm missing, possibly overlooked in the fs documentation.
const fs = require('fs');
let consumed = [];
const fConsume = () => {
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
let rawData = fs.readFileSync(`${testFolder}/${file}`);
let readable = JSON.parse(rawData);
consumed.push(readable);
});
})
}
fConsume();
console.log(consumed);
Just for clarification, this is what each JSON object looks like, and there are multiple objects per imported file.
{
id: 'a7294140-a453-4f3c-91b6-210819e2c43e',
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f0a1b070e0141070a1d010e010b0a152f02060c1d001c00091b410c0002">[email protected]</a>',
message: 'successfully handled skipped operation.'
},