So I want to save the titles array formatted with line breaks and index of each element.
When I save the 'formatted' object with fs all I get in the file is 'undefined'.
Also, this code is probably really messy and could be simplified, what should I change?
const $ = cheerio.load(body);
let titles = [];
$('div.collection.clearfix').each((i, el) => {
const title = $(el)
.text()
.replace(/\n/g, '')
.trim()
.replace(/ /g, ',');
titles.push(title);
})
let formatted = titles.forEach((i, title) => {
return title + '. ' + i + '\n'
});
fs.writeFile('titles.txt', formatted, (e) => {
if (e) console.error(e)
})