I would love to utilize a function that strips out items from something like .forEach(). An example of what I'm using currently looks like:
// return items even if no authentication is present
return ArticleModel.find(function (err, articles) {
var regularArray = [];
function stripOutPremium(element, index, array) {
if (element.is_premium === true) {
var elementDescAdjustment = element.description;
element.description = 'Premium content! ' + elementDescAdjustment.substr(0,15) + '...';
regularArray.push(element);
} else {
regularArray.push(element);
}
}
articles.forEach(stripOutPremium);
return res.send(regularArray);
However, this function stripOutPremium() is going to be used in a lot of different places, for this particular application (shown is a bit of a single Express App Route). How can I rewrite this function outside of this return statement so that I can use it on any route in my app? I know it involves doing something new with the var regularArray = []; and possibly even the prototype aspect of JS to do this.
Array.mapthen.