Skip to content

Collections

Zach Reedy edited this page Apr 28, 2020 · 1 revision

Collections

_.countBy(collection, [iteratee=_.identity])

Returns an object where the keys are generated via the results of invoking iteratee with each element of the collection. The values of each key of the returned object is the number of times the key was returned by the iteratee.

Parameters

  • collection collection: The collection to iterate over.
  • iteratee function (value): The iteratee to transform keys.

Returns

  • object: Returns the composed aggregate object.

Example

_.countBy([8.1, 5.2, 8.3], function (x) { return floor(x); });
// => { 5: 1, 8: 2 }

_.forEach(collection, [iteratee=_.identity])

Iterates over elements of collection and invokes iteratee for each element.

Parameters

  • collection collection: The collection to iterate over.
  • iteratee function (value, index|key, collection): The function invoked per iteration.

Returns

  • var: Returns collection.

Example

_.forEach([1, 2], function (value) {
    show_debug_message(string(value));
});
// Logs '1' then '2'

_.forEach({ a: 1, b: 2 }, function (value, key) {
    show_debug_message(key);
});
// Logs 'a' then 'b' (iteration order is not guarenteed).

_.forEachRight(collection, [iteratee=_.identity])

This method is like _.forEach except it iterates the collection from right to left.

Parameters

  • collection collection: The collection to iterate over.
  • iteratee function (value, index|key, collection): The function invoked per iteration.

Returns

  • var: Returns collection.

Example

_.forEach([1, 2], function (value) {
    show_debug_message(string(value));
});
// Logs '2' then '1'

_.every(collection, [predicate=_.identity])

Returns true if every element in the provided collection returns true when invoked with predicate.

Parameters

  • collection collection: The collection to iterate over.
  • predicate function (value, index|key, collection): The function invoked per iteration.

Returns

  • boolean: Returns true if all elements pass the predicate check, else false.

Example

_.every([1, 2, 3, 4], function (x) { return x < 5; });
// => true

var users = [
    { user: "foo", age: 25, active: false },
    { user: "bar", age: 29, active: false },
    { user: "baz", age: 33, active: false }
];
_.every(users, { user: "foo", active: false });
// => false

_.every(users, ["active",false]);
// => true

_.every(users, "active");
// => false

_.every(users, function (x) { return x.age < 50; });
// => true

_.filter(collection, [predicate=_.identity])

Returns a new collection given the provided collection containing only the elements from the original that result in true when invoked with predicate.

Parameters

  • collection collection: The collection to iterate over.
  • predicate function (value, index|key, collection): The function invoked per iteration.

Returns

  • collection: Returns the new filtered collection.

Example

var users = [
    { user: "foo", age: 25, active: true },
    { user: "bar", age: 29, active: false },
    { user: "baz", age: 29, active: true }
];

_.filter(users, function (x) { return !x.active; });
// => objects for ["bar"]

_.filter(users, { age: 25, active: true });
// => objects for ["foo"]

_.filter(users, ["active", true]);
// => objects for ["foo", "baz"]

_.filter(users, "active");
// => objects for ["foo", "baz"]

_.find(collection, [predicate=_.identity], [fromIndex=0])

Returns the first element found returning truthy when invoked with predicate. Iteration begins from fromIndex.

Parameters

  • collection collection: The collection to iterate over.
  • predicate function (value, index|key, collection): The function invoked per iteration.
  • fromIndex real: The index to search from.

Returns

  • var: Returns the matched element, else undefined.

Example

var users = [
    { user: "foo", age: 25, active: true },
    { user: "bar", age: 29, active: false },
    { user: "baz", age: 29, active: true }
];

_.find(users, function (x) { return !x.active; });
// => object for ["bar"]

_.find(users, { age: 29, active: false });
// => object for ["bar"]

_.find(users, ["active", true]);
// => object for "foo"

_.find(users, "active");
// => object for "foo"

_.find(users, "active", 1);
// => object for "baz"

_.findLast(collection, [predicate=_.identity], [fromIndex=collection.length-1])

Same as _.find except it iterates the collection from right to left.

Parameters

  • collection collection: The collection to iterate over.
  • predicate function (value, index|key, collection): The function invoked per iteration.
  • fromIndex real: The index to search from.

Returns

  • var: Returns the matched element, else undefined.

Example

_.findLast([1,2,3,4], function (x) {
    return x % 2 == 1;
});
// => 3

_.flatMap(collection, [iteratee=_.identity])

Returns a new flattend collection of values by iterating through the provided collection and invoking each element with iteratee. The resulting collection is then concatenated with the result.

Parameters

  • collection collection: The collection to iterate over.
  • iteratee function (value, index|key, collection): The function invoked per iteration.

Returns

  • collection: Returns the new flattened collection.

Example

_.flatMap([1,2], function (x) { return [x, x]; });
// => [ 1, 1, 2, 2 ]

_.flatMapDeep(collection, [iteratee=_.identity])

Same as _.flatMap except it recursively flattens the mapped results.

Parameters

  • collection collection: The collection to iterate over.
  • iteratee function (value, index|key, collection): The function invoked per iteration.

Returns

  • collection: Returns the new flattened collection.

Example

_.flatMapDeep([1,2], function (x) { return [[x, x], x]; });
// => [ 1, 1, 1, 2, 2, 2 ]

_.flatMapDeep(collection, [iteratee=_.identity], [depth=1])

Same as _.flatMap except it recursively flattens the mapped results up to depth times.

Parameters

  • collection collection: The collection to iterate over.
  • iteratee function (value, index|key, collection): The function invoked per iteration.
  • depth real: The maximum recursion depth.

Returns

  • collection: Returns the new flattened collection.

Example

_.flatMapDeep([1,2], function (x) { return [[[x, x]]]; }, 2);
// => [ [1, 1], [2, 2] ]

_.groupBy(collection, [iteratee=_.identity])

Creates an object where the keys are the result of iterating through the provided collection and invoking iteratee with each element. The value of each key is a collection of elements responsible for generating the key.

Parameters

  • collection collection: The collection to iterate over.
  • iteratee function (value): The function invoked per iteration.
  • depth real: The maximum recursion depth.

Returns

  • object: Returns the generated aggregate object.

Example

_.groupBy([8.1, 5.2, 8.3], function (x) { return floor(x); });
// => { 8: [8.1, 8.3], 5: [5.2] }

var users = [
    { user: "foo", age: 25, active: true },
    { user: "bar", age: 29, active: false },
    { user: "baz", age: 29, active: true }
];
_.groupBy(users, "age");
// => { 25: [{user:"foo",...}], 29: [{user:"bar",...}, {user:"baz",...}] }

_.includes(collection, value, [fromIndex=0])

Returns if a value is in the provided collection. If collection type is a string, then it's check for a substring, otherwise _.isEqual is used for equality comparisons.

Parameters

  • collection collection: The collection to iterate over.
  • value var: The value to search for.
  • fromIndex real: The maximum recursion depth.

Returns

  • boolean: Returns true if value is found, else false.

Example

_.includes([1,2,3,4], 1);
// => true

_.includes([1,2,3,4], 1, 2);
// => false

_.includes({ a: 1, b: 2 }, 1);
// => true

_.includes("Hello", "ll");
// => true

_.keyBy(collection, [iteratee=_.identity])

Creates an object where the keys are the result of iterating through the provided collection and invoking iteratee with each element. The value of each key is the value of the last element that resulting in its key.

Parameters

  • collection collection: The collection to iterate over.
  • iteratee function (value): The iteratee to transform keys.

Returns

  • object: Returns the generated aggregate object.

Example

var array = [
    { dir: "left", code: 97 },
    { dir: "right", code: 100 }
];

_.keyBy(array, function (x) { return chr(x); });
// => { a: { dir: "left", code: 97 }, b: { dir: "right", code: 100 } }

_.keyBy(array, "dir");
// => { left: { dir: "left", code: 97 }, right: { dir: "right", code: 100 } }

_.map(collection, [iteratee=_.identity])

Creates a collection of values by iterating through the provided collection and invoking iteratee with each element. The result being the new element value for the returned collection.

Parameters

  • collection collection: The collection to iterate over.
  • iteratee function (value, index|key, collection): The iteratee to transform keys.

Returns

  • collection: Returns the new mapped collection.

Example

_.map([1,2,3], function (x) { return x * x; });
// => [1, 4, 9]

_.map({ a: 4, b: 8 }, function (x) { return x * x; });
// => [16, 64] (iteration order is not guarenteed)

var users = [
    { user: "foo", age: 25, active: true },
    { user: "bar", age: 29, active: false },
    { user: "baz", age: 29, active: true }
];

_.map(users, "user");
// => ["foo", "bar", "baz"]

// Naive rot13
_.map("hello", function (x) { return chr($60 + (ord(x) - $60 + 13) % 26); });
// => ["u","r","y","y","b"]

_.partition(collection, [predicate=_.identity])

Creates a collection of elements split into two groups, the first contains all elements the predicate returns false for, the second contains all elements returned true for.

Parameters

  • collection collection: The collection to iterate over.
  • iteratee function (value): The iteratee to determine binning.

Returns

  • collection: Returns the collection of grouped elements.

Example

var users = [
    { user: "foo", age: 25, active: true },
    { user: "bar", age: 29, active: false },
    { user: "baz", age: 29, active: true }
];

_.partition(users, function (x) { return x.active; });
// => objects for [["bar"], ["foo","baz"]]

_.partition(users, {age: 29, active: true});
// => objects for [["foo","bar"], ["baz"]]

_.partition(users, ["active",false]);
// => objects for [["foo","baz"], ["bar"]]

_.partition(users, "active");
// => objects for [["bar"], ["foo","baz"]]

_.reduce(collection, [iteratee=_.identity], [accumulator])

Reduces collection to a value which is the accumulated result of iterating through the provided collection and invoking iteratee with the element. Successive invocations are supplied the return value of the previous. If an accumulator is not provided, the first element of the provided collection is used as the initial value.

Parameters

  • collection collection: The collection to iterate over.
  • iteratee function (accumulator, value, index|key, collection): The function invoked per iteration.
  • accumulator var: The initial value.

Returns

  • var: Returns the accumulated value.

Example

_.reduce([1,2], function (sum, x) { return sum + x; }, 0);
// => 3

_.reduce({a:1, b:2, c:1}, function (result, value, key) {
	var sub;
	if (!variable_struct_exists(result, value)) {
		sub = [];
		variable_struct_set(result, value, sub);
	}
	else
		sub = variable_struct_get(result, value);
		sub[@ array_length(sub)] = key; 
		return result;
	}, {});
// => { 1: ["a","c"], 2: ["b"] }

_.reduceRight(collection, [iteratee=_.identity], [accumulator])

Same as _.reduce except that it iterates over elements from right to left.

Parameters

  • collection collection: The collection to iterate over.
  • iteratee function (accumulator, value, index|key, collection): The function invoked per iteration.
  • accumulator var: The initial value.

Returns

  • var: Returns the accumulated value.

Example

var array = [[0, 1], [2, 3], [4, 5]];
 
_.reduceRight(array, function (result, other) {
  for (var i = 0; i < array_length(other); ++i)
      result[@ array_length(result)] = other[i];
  return result;
}, []);
// => [4, 5, 2, 3, 0, 1]

_.shuffle(collection)

Returns a new collection of shuffled values, given the provided collection. Uses Fisher-Yates shuffle as the algorithm.

Parameters

  • collection collection: The collection to iterate over.

Returns

  • collection: Returns the new shuffled collection.

Example

_.shuffle([1,2,3,4]);
// => [4, 1, 3, 2]

_.size(value)

Returns the size of the value. For collections, this is the number of elements. For objects, this is the number of properties. For strings, this is the length.

Parameters

  • value collection|object|string: The value to inspect.

Returns

  • real: Returns the value's size.

Example

_.size([1,2,3]);
// => 3
 
_.size({ a: 1, b: 2 });
// => 2
 
_.size("foobar");
// => 6

_.some(collection, [predicate=_.identity])

Returns if predicate returns true for any element in the provided collection.

Parameters

  • collection collection: The collection to iterate over.
  • iteratee function (value, index|key, collection): The function invoked per iteration.

Returns

  • boolean: Returns true if any element passes the predicate check, else false.

Example

_.some([1,2,3,4], function (x) { return x > 2; });
// => true

var users = [
    { user: "foo", age: 25, active: true },
    { user: "bar", age: 29, active: false },
    { user: "baz", age: 29, active: true }
];

_.some(users, { user: "foo", age: 25 });
// => true

_.some(users, { user: "foo", age: 26 });
// => false

_.some(users, ["active", false]);
// => true

_.some(users, "active");
// => true

_.some("foobar", function (x) { return x == "o"; });
// => true

_.join(collection, [separator=","])

Converts all elements in a collection into a string separated by separator. This function is recursive.

Parameters

  • collection collection: The collection to convert.
  • separator string: The element separator.

Returns

  • string: Returns the joined string.

Example

_.join([1,2,3]);
// => "1,2,3"

_.join([1,2,3,[4,5]]);
// => "1,2,3,4,5"

_.join(["a","b","c"], "~");
// => a~b~c

Clone this wiki locally