Recently I wrote some code to take two arrays of data and create a title. Each array could have 0-n elements. However, the multiple if
statements seem messy to me. Is there a nicer way to do this? Perhaps one that harnesses the power of jQuery better?
var campaign_names = data["campaign_names"];
var list_names = data["list_names"];
var title;
if (campaign_names && campaign_names.length > 1) {
campaign_names = campaign_names.join(", ");
} else if (campaign_names) {
campaign_names = campaign_names[0];
}
if (list_names && list_names.length > 1) {
list_names = list_names.join(", ");
} else if (list_names) {
list_names = list_names[0];
}
if (campaign_names && list_names) {
title = campaign_names + " & " + list_names;
} else if (campaign_names) {
title = campaign_names;
} else if (list_names) {
title = list_names;
}