jQuery.each for lists and non-lists
- by Brian M. Hunt
I've a jQuery.each(data, foo), where data is either a string or a list of strings. I'd like to know if there's an existing utility function to convert the string to a list, or otherwise perform foo on just the string. So instead of the easy route:
if (!$.isArray(data)) {
foo(0, data); // can't rely on `this` variable
} else {
$.each(data,foo);
}
I was just wondering if there was already a builtin function of jQuery or Javascript that would convert data to a list automatically, like this:
function convert_to_list(data) { return $.isArray(data) ? data : [data]; }
$.each(convert_to_list(data), foo);
Just curious!
Thanks for reading.
Brian