I have a function that combines an array of objects with a conditional delimiter.
function getSegmentsLabel(segments) {
var separator = '-';
var segmentsLabel = '';
var nextSeparator = '';
_.forEach(segments, function(segment) {
segmentsLabel += nextSeparator + segment.label;
nextSeparator = segment.separatorUsed ? separator : ' ';
});
return segmentsLabel;
}
Customs:
var segments = [
{label: 'First', separatorUsed: true},
{label: 'Second', separatorUsed: false},
{label: 'Third', separatorUsed: true},
{label: 'Forth', separatorUsed: true}
];
getSegmentsLabel(segments);
How can one write the above function in a getSegmentsLabelpurely functional way without mutating variables? We can use the lodash functions.
source
share