back to Overview


Creates a new Minified list from the current list using the given callback function.
Available in all modules.

Syntax Variants

list.collect(collectFunc, ctx)
_.collect(list, collectFunc)
_.collect(list, collectFunc, ctx)


a list to transform. Can be an array, a Minified list or any other array-like structure with length property.
The callback function(item, index) to invoke for each item:
The current list element.
The second the zero-based index of the current element.
The given context if not null. Otherwise the list.
(callback return value)
If the callback returns a list, its elements will be added to the result list. Other objects will also be added. Nulls and undefined will be ignored and not be added to the new result list.
ctx (optional)
a context to pass to the callback as 'this'. Only supported in UTIL module.
(return value)
the new listinput and output list.


Creates a new Minified list from the current list using the given callback function. The callback is invoked once for each element of the current list. The callback results will be added to the result list. The callback can return

  • an array or another list-like object. Its content will be appended to the resulting list.
  • a regular object which will be appended to the list
  • null (or undefined), which means that no object will be added to the list. If you need to add null or undefined to the result list, put it into a single-element array.


Goes through a list of numbers. Numbers over 10 will be removed. Numbers 5 and below stay. Numbers between 6 and 10 will be replaced by two numbers whose sum is the original value.

var texts = _(3, 7, 11, 5, 19, 3).collect(function(number, index) {
    if (number > 10)
        return null;           // remove numbers >10
    else if (number > 5)
        return [5, number-5];  // replace with two entries
        return number;         // keep lower numbers


Goes through input elements on a web page. If they are text inputs, their value will be added to the list:

var texts = $('input').collect(function(input) {
    if (input.getAttribute('type') != null || input.getAttribute('type') == 'text')
        return input.value;
        return null; // ignore


Creates a list of all children of the selected list.

var childList = $('.mySections').collect(function(node) {
    return node.childNodes; // adds a while list of nodes


Goes through selected input elements. For each hit, the innerHTML is added twice, once in lower case and once in upper case:

var elements = $('input.myTexts').collect(function(item) {
    return [item.innerHTML.toLowerCase(), item.innerHTML.toUpperCase()];

See also..

  • map() is a simpler version of collect() that can be useful if there is a 1:1 mapping between


comments powered by Disqus

back to Overview