I want to implement the filter and map approach rather than using a {for} loop to iterate through an array.
Here is my previous code. I was filtering matching strings from an array and combining them.
function oldFunction(data) {
let dataDeleted = '';
let each
for (each of data) {
if (each.includes('_DELETE')) {
dataDeleted = dataDeleted + each.substring(0, each.length - 7) + '###';
}
}
dataDeleted = dataDeleted;
console.log(dataDeleted);
}
oldFunction(['meow_DELETE', 'haga', 'Neigh_DELETE']);
The result is: 'meow###Neigh###'
Now I attempted using the 'filter' and 'reduce' methods, but it doesn't work as expected:
function newMethod(data) {
const dataDeleted = data
.filter(d => d.includes('_DELETE'))
.reduce((deletedData, d) => {
deletedData.concat(`${d}### `);
}, '');
console.log(dataDeleted);
return dataDeleted;
}
Instead of getting the desired output, this returns 'undefined'. Any advice? Thank you in advance.