I would like to know if jQuery or a jQuery plugin has a function that does what the following function does:
function $array(a /* an ARRAY */, f /* a function */) {
var $res = $();
for (var i = 0; i < a.length; ++i)
/* Notice: The result of evaluating f(a[i])
* shall always be a jQuery selector
*/
$res.add(f(a[i]));
return $res;
}
I find this function very useful, but I don’t want to reimplement it if it already exists.
EDIT 1:
I admit the question was not particularly clear. I want to be able to do something like:
$('<select>')
.append($array([
{ value: 0, name: 'Item 0' },
{ value: 1, name: 'Item 1' },
{ value: 2, name: 'Item 2' }
], function(option) {
return $('<option>')
.val(option.value)
.html(option.name);
}))
.appendTo('body');
I think this is map.
Actually, that’s not quite right if the input of your function is not a jQuery object. There’s also jQuery.map, which you could use to make an array-of-jQuery-objects, but doesn’t return a jQuery object itself. So I think neither is exactly what you’ve got there.