bower
Version:
The browser package manager
234 lines (144 loc) • 4.54 kB
Markdown
Methods for dealing with collections (Array or Objects).
## contains(list, value):Boolean
Checks if collection contains value.
```js
contains({a: 1, b: 2, c: 'bar'}, 2); // true
contains([1, 2, 3], 'foo'); // false
```
See: [array/contains](array.html
Tests whether all values in the collection pass the test implemented by the
provided callback.
```js
var obj = {
a: 1,
b: 2,
c: 3,
d: 'string'
};
every(obj, isNumber); // false
```
See: [array/every](array.html
Filter collection properties.
See: [array/filter](array.html
Loops through all the values in the collection and returns the first one that
passes a truth test (callback).
**Important:** loop order over objects properties isn't guaranteed to be the
same on all environments.
```js
find({a: 'foo', b: 12}, isString); // 'foo'
find(['foo', 12], isNumber); // 12
```
See: [array/find](array.html
Loop through all values of the collection.
See: [array/forEach](array.html
Returns a new collection where the properties values are the result of calling
the callback for each property in the original collection.
See: [array/map](array.html
Returns maximum value inside collection or use a custom iterator to define how
items should be compared.
See: [`min()`](
```js
max({a: 100, b: 2, c: 1, d: 3, e: 200}); // 200
max(['foo', 'lorem', 'amet'], function(val){
return val.length;
}); // 'lorem'
```
Returns minimum value inside collection or use a custom iterator to define how
items should be compared.
See: [`max()`](
```js
min([10, 2, 7]); // 2
min({a: 'foo', b: 'lorem', c: 'amet'}, function(val){
return val.length;
}); // 'foo'
```
Extract a list of property values.
```js
var users = [
{
name : 'John',
age : 21
},
{
name : 'Jane',
age : 27
}
];
pluck(users, 'name'); // ["John", "Jane"]
pluck(users, 'age'); // [21, 27]
users = {
first: {
name : 'John',
age : 21
},
second: {
name : 'Mary',
age : 25
}
};
pluck(users, 'name'); // ['John', 'Mary']
```
See: [array/pluck](array.html
Apply a function against an accumulator and each value in the collection as to
reduce it to a single value.
```js
var obj = {a: 1, b: 2, c: 3, d: 4};
function sum(prev, cur, key, list) {
return prev + cur;
}
reduce(obj, sum); // 10
```
See: [array/reduce](array.html
Creates a new array with all the elements that do **not** pass the truth test.
Opposite of [`filter()`](
```js
var numbers = [1, 2, 3, 4, 5];
reject(numbers, function(x) { return (x % 2) !== 0; }); // [2, 4]
var obj = {a: 1, b: 2, c: 3, d: 4, e: 5};
reject(obj, function(x) { return (x % 2) !== 0; }); // [2, 4]
```
See: [array/reject](array.html
Returns the number of values in the collection.
```js
var obj = {
foo : 1,
bar : 2,
lorem : 3
};
size(obj); // 3
size([1,2,3]); // 3
size(null); // 0
```
See: [object/size](object.html
Tests whether any values in the collection pass the test implemented by the
provided callback.
```js
var obj = {
a: 1,
b: 2,
c: 3,
d: 'string'
};
some(obj, isNumber); // true
some(obj, isString); // true
some([1, 2, 3], isNumber) // true
some([1, 2, 3], isString) // false
```
See: [array/some](array.html
-------------------------------------------------------------------------------
For more usage examples check specs inside `/tests` folder. Unit tests are the
best documentation you can get...