Lightweight, use-anywhere toolkit for working with array data.
npm install array-tools





There are four ways to use it.
1) As a command-line tool. E.g. array-tools downloads last month:
``sh`
$ curl -s https://api.npmjs.org/downloads/range/last-month/array-tools \
| object-tools get downloads \
| array-tools pluck downloads \
| array-tools join "," \
| spark
▂▅▃▅▅▁▁▃▄▃▆▂▂▁▁▂▄▃▃▁▁▂█▆▆▄▁▃▅▃
2) As a standard library, passing the input array on each method invocation:
`js
> var a = require("array-tools");
> var remainder = a.without([ 1, 2, 3, 4, 5 ], 1)
> a.exists(remainder, 1)
false
`
3) As a chainable method, passing the input array once then chaining from there:
`js`
> a([ 1, 2, 3, 4, 5 ]).without(1).exists(1);
false
4) As a base class.
`js
var util = require("util");
var ArrayTools = require("array-tools");
// this class will inherit all array-tools methods
function CarCollection(cars){
ArrayTools.call(this, cars);
}
util.inherits(CarCollection, ArrayTools);
var cars = new CarCollection([
{ owner: "Me", model: "Citreon Xsara" },
{ owner: "Floyd", model: "Bugatti Veyron" }
]);
cars.findWhere({ owner: "Floyd" });
// returns { owner: "Floyd", model: "Bugatti Veyron" }
`
#### More on chaining
* Each method returning an Array (e.g. where, without) can be chained.exists
* Methods not returning an array (, contains) cannot be chained.Array.prototype
* All methods from (e.g. .join, .forEach etc.) are also available in the chain. The same rules, regarding what can and cannot be chained, apply as above..val()
* If the final operation in your chain is "chainable" (returns an array), append to terminate the chain and retrieve the output.
`js`
> a([ 1, 2, 2, 3 ]).exists(1)
true
> a([ 1, 2, 2, 3 ]).without(1).exists(1)
false
> a([ 1, 2, 2, 3 ]).without(1).unique().val()
[ 2, 3 ]
> a([ 1, 2, 2, 3 ]).without(1).unique().join("-")
'2-3'
``
$ npm install array-tools --save
As a command-line tool:
``
$ npm install -g array-tools
Using bower:
``
$ bower install array-tools --save
* array-tools
* _chainable_
* .arrayify(any) ⇒ Array
* .where(array, query) ⇒ Array
* .without(array, toRemove) ⇒ Array
* .pluck(recordset, property) ⇒ Array
* .pick(recordset, property) ⇒ Array.<object>
* .unique(array) ⇒ Array
* [.spliceWhile(array, index, test, [...elementN])](#module_array-tools.spliceWhile) ⇒ Array
* .extract(array, query) ⇒ Array
* .flatten(array) ⇒ Array
* .sortBy() ⇒ Array
* _not chainable_
* .exists(array, query) ⇒ boolean
.findWhere(recordset, query) ⇒ \
.remove(arr, toRemove) ⇒ \
.last(arr) ⇒ \
* .contains(array, value) ⇒ boolean
- converts array-like objects (e.g. arguments) to a real arrayundefined
- converts to an empty arraynull
- converts any another other, singular value (including ) into an array containing that value
- ignores input which is already an array
Kind: static method of array-tools
Category: chainable
| Param | Type | Description |
| --- | --- | --- |
| any | \* | the input value to convert to an array |
Example
`js
> a.arrayify(undefined)
[]
> a.arrayify(null)
[ null ]
> a.arrayify(0)
[ 0 ]
> a.arrayify([ 1, 2 ])
[ 1, 2 ]
> function f(){ return a.arrayify(arguments); }
> f(1,2,3)
[ 1, 2, 3 ]
`$3
Deep query an array.
Kind: static method of array-tools
Category: chainable
| Param | Type | Description |
| --- | --- | --- |
| array | Array.<object> | the array to query |
| query | any | Array.<any> | one or more queries |
Example
Say you have a recordset:
`js`
> data = [
{ name: "Dana", age: 30 },
{ name: "Yana", age: 20 },
{ name: "Zhana", age: 10 }
]
You can return records with properties matching an exact value:
`js`
> a.where(data, { age: 10 })
[ { name: 'Zhana', age: 10 } ]
or where NOT the value (prefix the property name with !)`js`
> a.where(data, { "!age": 10 })
[ { name: 'Dana', age: 30 }, { name: 'Yana', age: 20 } ]
match using a function:
`js`
> function over10(age){ return age > 10; }
> a.where(data, { age: over10 })
[ { name: 'Dana', age: 30 }, { name: 'Yana', age: 20 } ]
match using a regular expression
`js`
> a.where(data, { name: /ana/ })
[ { name: 'Dana', age: 30 },
{ name: 'Yana', age: 20 },
{ name: 'Zhana', age: 10 } ]
You can query to any arbitrary depth. So with deeper data, like this:
`js`
> deepData = [
{ name: "Dana", favourite: { colour: "light red" } },
{ name: "Yana", favourite: { colour: "dark red" } },
{ name: "Zhana", favourite: { colour: [ "white", "red" ] } }
]
get records with favourite.colour values matching /red/`js`
> a.where(deepData, { favourite: { colour: /red/ } })
[ { name: 'Dana', favourite: { colour: 'light red' } },
{ name: 'Yana', favourite: { colour: 'dark red' } } ]
if the value you're looking for _maybe_ part of an array, prefix the property name with +. Now Zhana is included:`js`
> a.where(deepData, { favourite: { "+colour": /red/ } })
[ { name: 'Dana', favourite: { colour: 'light red' } },
{ name: 'Yana', favourite: { colour: 'dark red' } },
{ name: 'Zhana', favourite: { colour: [ "white", "red" ] } } ]
you can combine any of the above by supplying an array of queries. Records will be returned if _any_ of the queries match:
`js
> var nameBeginsWithY = { name: /^Y/ }
> var faveColourIncludesWhite = { favourite: { "+colour": "white" } }
> a.where(deepData, [ nameBeginsWithY, faveColourIncludesWhite ])
[ { name: 'Yana', favourite: { colour: 'dark red' } },
{ name: 'Zhana', favourite: { colour: [ "white", "red" ] } } ]
`$3
Returns a new array with the same content as the input minus the specified values. It accepts the same query syntax as where.
Kind: static method of array-tools
Category: chainable
| Param | Type | Description |
| --- | --- | --- |
| array | Array | the input array |
| toRemove | any | Array.<any> | one, or more queries |
Example
`js
> a.without([ 1, 2, 3 ], 2)
[ 1, 3 ]
> a.without([ 1, 2, 3 ], [ 2, 3 ])
[ 1 ]
> data = [
{ name: "Dana", age: 30 },
{ name: "Yana", age: 20 },
{ name: "Zhana", age: 10 }
]
> a.without(data, { name: /ana/ })
[]
`$3
Returns an array containing each value plucked from the specified property of each object in the input array.
Kind: static method of array-tools
Category: chainable
| Param | Type | Description |
| --- | --- | --- |
| recordset | Array.<object> | The input recordset |
| property | string | Array.<string> | Property name, or an array of property names. If an array is supplied, the first existing property will be returned. |
Example
with this data..
`js`
> var data = [
{ name: "Pavel", nick: "Pasha" },
{ name: "Richard", nick: "Dick" },
{ name: "Trevor" },
]
pluck all the nicknames
`js`
> a.pluck(data, "nick")
[ 'Pasha', 'Dick' ]
in the case no nickname exists, take the name instead:
`js`
> a.pluck(data, [ "nick", "name" ])
[ 'Pasha', 'Dick', 'Trevor' ]
the values being plucked can be at any depth:
`js
> var data = [
{ leeds: { leeds: { leeds: "we" } } },
{ leeds: { leeds: { leeds: "are" } } },
{ leeds: { leeds: { leeds: "Leeds" } } }
]
> a.pluck(data, "leeds.leeds.leeds")
[ 'we', 'are', 'Leeds' ]
`recordset$3
return a copy of the input containing objects having only the cherry-picked properties
Kind: static method of array-tools
Category: chainable
| Param | Type | Description |
| --- | --- | --- |
| recordset | Array.<object> | the input |
| property | string | Array.<string> | the properties to include in the result |
Example
with this data..
`js`
> data = [
{ name: "Dana", age: 30 },
{ name: "Yana", age: 20 },
{ name: "Zhana", age: 10 }
]
return only the "name" field..`js`
> a.pick(data, "name")
[ { name: 'Dana' }, { name: 'Yana' }, { name: 'Zhana' } ]
return both the "name" and "age" fields`js`
> a.pick(data, [ "name", "age" ])
[ { name: 'Dana', age: 30 },
{ name: 'Yana', age: 20 },
{ name: 'Zhana', age: 10 } ]
cherry-picks fields at any depth:
`js
> data = [
{ person: { name: "Dana", age: 30 }},
{ person: { name: "Yana", age: 20 }},
{ person: { name: "Zhana", age: 10 }}
]
> a.pick(data, "person.name")
[ { name: 'Dana' }, { name: 'Yana' }, { name: 'Zhana' } ]
> a.pick(data, "person.age")
[ { age: 30 }, { age: 20 }, { age: 10 } ]
`$3
Returns an array containing the unique values from the input array.
Kind: static method of array-tools
Category: chainable
| Param | Type | Description |
| --- | --- | --- |
| array | Array | input array |
Example
`js`
> a.unique([ 1, 6, 6, 7, 1])
[ 1, 6, 7 ]$3
Splice items from the input array until the matching test fails. Returns an array containing the items removed.
Kind: static method of array-tools
Category: chainable
| Param | Type | Description |
| --- | --- | --- |
| array | Array | the input array |
| index | number | the position to begin splicing from |
| test | any | the sequence of items passing this test will be removed |
| [...elementN] | \* | elements to add to the array in place |
Example
`js
> function under10(n){ return n < 10; }
> numbers = [ 1, 2, 4, 6, 12 ]
> a.spliceWhile(numbers, 0, under10)
[ 1, 2, 4, 6 ]
> numbers
[ 12 ]
> countries = [ "Egypt", "Ethiopia", "France", "Argentina" ]
> a.spliceWhile(countries, 0, /^e/i)
[ 'Egypt', 'Ethiopia' ]
> countries
[ 'France', 'Argentina' ]
`array$3
Removes items from which satisfy the query. Modifies the input array, returns the extracted.
Kind: static method of array-tools
Returns: Array - the extracted items.
Category: chainable
| Param | Type | Description |
| --- | --- | --- |
| array | Array | the input array, modified directly |
| query | any | if an item in the input array passes this test it is removed |
Example
`js
> DJs = [
{ name: "Trevor", sacked: true },
{ name: "Mike", sacked: true },
{ name: "Chris", sacked: false },
{ name: "Alan", sacked: false }
]
> a.extract(DJs, { sacked: true })
[ { name: 'Trevor', sacked: true },
{ name: 'Mike', sacked: true } ]
> DJs
[ { name: 'Chris', sacked: false },
{ name: 'Alan', sacked: false } ]
`$3
flatten an array of arrays into a single array.
Kind: static method of array-tools
Category: chainable
Since: 1.4.0
| Param | Type | Description |
| --- | --- | --- |
| array | Array | the input array |
Example
`js`
> numbers = [ 1, 2, [ 3, 4 ], 5 ]
> a.flatten(numbers)
[ 1, 2, 3, 4, 5 ]$3
Sort an array of objects by one or more fields
Kind: static method of array-tools
Category: chainable
Since: 1.5.0
| Type | Description |
| --- | --- |
| Array.<object> | input array |
| string | Array.<string> | column name(s) to sort by |
| object | specific sort orders, per columns |
Example
with this data
`js`
> DJs = [
{ name: "Trevor", slot: "twilight" },
{ name: "Chris", slot: "twilight" },
{ name: "Mike", slot: "afternoon" },
{ name: "Rodney", slot: "morning" },
{ name: "Chris", slot: "morning" },
{ name: "Zane", slot: "evening" }
]
sort by slot using the default sort order`js`
> a.sortBy(DJs, "slot")
[ { name: 'Mike', slot: 'afternoon' },
{ name: 'Zane', slot: 'evening' },
{ name: 'Chris', slot: 'morning' },
{ name: 'Rodney', slot: 'morning' },
{ name: 'Chris', slot: 'twilight' },
{ name: 'Trevor', slot: 'twilight' } ]
specify a custom sort order for slot`js`
> a.sortBy(DJs, "slot", { slot: [ "morning", "afternoon", "evening", "twilight" ]})
[ { name: 'Rodney', slot: 'morning' },
{ name: 'Chris', slot: 'morning' },
{ name: 'Mike', slot: 'afternoon' },
{ name: 'Zane', slot: 'evening' },
{ name: 'Trevor', slot: 'twilight' },
{ name: 'Chris', slot: 'twilight' } ]
sort by slot then name`js`
> a.sortBy(DJs, ["slot", "name"], { slot: [ "morning", "afternoon", "evening", "twilight" ]})
[ { name: 'Chris', slot: 'morning' },
{ name: 'Rodney', slot: 'morning' },
{ name: 'Mike', slot: 'afternoon' },
{ name: 'Zane', slot: 'evening' },
{ name: 'Chris', slot: 'twilight' },
{ name: 'Trevor', slot: 'twilight' } ]$3
Works in exactly the same way as where but returning a boolean indicating whether a matching record exists.
Kind: static method of array-tools
Category: not chainable
| Param | Type | Description |
| --- | --- | --- |
| array | Array | the array to search |
| query | \* | the value to search for |
Example
`js
> data = [
{ name: "Dana", age: 30 },
{ name: "Yana", age: 20 },
{ name: "Zhana", age: 10 }
]
> a.exists(data, { age: 10 })
true
`$3
Works in exactly the same way as where but returns only the first item found.
Kind: static method of array-tools
Category: not chainable
| Param | Type | Description |
| --- | --- | --- |
| recordset | Array.<object> | the array to search |
| query | object | the search query |
Example
`js
> dudes = [
{ name: 'Jim', age: 8 },
{ name: 'Clive', age: 8 },
{ name: 'Hater', age: 9 }
]
> a.findWhere(dudes, { age: 8 })
{ name: 'Jim', age: 8 }
`$3
Removes the specified value from the input array.
Kind: static method of array-tools
Category: not chainable
Since: 1.8.0
| Param | Type | Description |
| --- | --- | --- |
| arr | Array | the input array |
| toRemove | \* | the item to remove |
Example
`js
> numbers = [ 1, 2, 3 ]
> a.remove(numbers, 1)
[ 1 ]
> numbers
[ 2, 3 ]
`$3
Return the last item in an array.
Kind: static method of array-tools
Category: not chainable
Since: 1.7.0
| Param | Type | Description |
| --- | --- | --- |
| arr | Array | the input array |
Kind: static method of array-tools
Category: not chainable
Since: 1.8.0
| Param | Type | Description |
| --- | --- | --- |
| array | Array | the input array |
| value | \* | the value to look for |
*
© 2015-16 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.