EDIT: Expanding on the accepted answer (deduplicating entries):
["cat, Kitchen", "dog", "Kitchen, dog", "Kitchen"].join(",").split(",").
map(function(entry){return entry.trim();}).
filter(function(entry, i, a) { return a.indexOf(entry) === i; });
I solved it with a series of transformations, no need for jQuery.
Couldn't name it better, though, as I don't know what's the real deal.
function doItsThing (crazyArray) {
function toArrays (entry) {
return entry.split(',');
}
function toAFlatArray (arrays, entry) {
return arrays.concat(entry);
}
function trim (entry) {
return entry.trim();
}
function dedupe (entry, index, array) {
return array.indexOf(entry) == index;
}
return crazyArray.
map(toArrays).
reduce(toAFlatArray, []).
map(trim).
filter(dedupe);
}
doItsThing(["cat, Kitchen", "dog", "Kitchen, dog", "Kitchen"])
Or, if you want it all inlined (although not so readable, compared to the above, imo):
function doItsThing (crazyArray) {
return crazyArray.
map(function (entry) {
return entry.split(',');
}).
reduce(function (collection, entry) {
return collection.concat(entry);
}, []).
map(function (entry) {
return entry.trim();
}).
filter(function (entry, index, collection) {
return collection.indexOf(entry) == index;
});
}
doItsThing(["cat, Kitchen", "dog", "Kitchen, dog", "Kitchen"])
Sure it can be improved, but this is the idea.
catand before the 2nddogintentional?