I'm trying to show the filters selected from a checkbox. First I'm creating an array of values selected from the checkboxes and I'm trying to append to a string
For example I have two checkboxes labeled: label1 and label2, I built the array ['label1','label2'].
The desired result would be "Filtered by: Label1, Label2" but instead I get Filtered by: Label1,Label2, Label1,Label2
I guess it's something in the for loop where I'm trying to build the string, because the array looks fine
let topLabel = jQuery('.industry-filter').data('selected-text') + ' ';
let termsName= ['Industry', 'Category']
if(termsName.length){
for(var j = 0; j < termsName.length; j++){
if(j == termsName.length - 1){
topLabel += termsName;
}else{
topLabel += termsName+', ';
}
}
}else{
topLabel = jQuery('.industry-filter').data('text');
}
$('.industry-filter').text(topLabel);
Here is a pen that shows the issue: https://codepen.io/anon/pen/pqjYxL
if(j == termsName.length - 1){ topLabel += termsName[j]; }else{ topLabel += termsName[j]+', '; }termsName.join(", ", termsName)you're just making your code complicated