Package Exports
- array-tools
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (array-tools) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
#array-tools Useful functions for working with arrays
####Example
var a = require("array-tools");Contents
- pluck(arrayOfObjects, ...property)
- arrayify(input)
- exists(array, value)
- where(arrayOfObjects, query)
- findWhere(arrayOfObjects, query)
- without(input, toRemove)
- union(array1, array2, idKey)
- commonSequence(a, b)
- unique(array)
- spliceWhile(array, index, test, ...elementN)
###pluck(arrayOfObjects, ...property) Plucks the value of the specified property from each object in the input array
- arrayOfObjects
Array.<Object>the input array of objects - ...property
stringthe property(s) to pluck
Returns: Array
####Example
> var data = [
... {one: 1, two: 2},
... {two: "two"},
... {one: "one", two: "zwei"},
... ];
undefined
> a.pluck(data, "one");
[ 1, 'one' ]
> a.pluck(data, "two");
[ 2, 'two', 'zwei' ]
> a.pluck(data, "one", "two");
[ 1, 'two', 'one' ]###arrayify(input) Takes input and guarantees an array back. Result can be one of three things:
puts a single scalar in an array
converts array-like object (e.g.
arguments) to a real arrayconverts
nullorundefinedto an empty arrayinput
*the input value to convert to an array
Returns: Array
####Example
> a.arrayify(null)
[]
> a.arrayify(0)
[ 0 ]
> a.arrayify([ 1, 2 ])
[ 1, 2 ]
> function f(){ return a.arrayify(arguments); }
undefined
> f(1,2,3)
[ 1, 2, 3 ]###exists(array, value) returns true if a value, or nested object value exists in an array
- array
Arraythe array to search - value
*the value to search for
Returns: boolean
####Example
> a.exists([ 1, 2, 3 ], 2)
true
> a.exists([ { result: false }, { result: false } ], { result: true })
false
> a.exists([ { result: true }, { result: false } ], { result: true })
true
> a.exists([ { result: true }, { result: true } ], { result: true })
true
###where(arrayOfObjects, query)
returns an array containing items from arrayOfObjects where key/value pairs
from query are matched identically
- arrayOfObjects
Arraythe array to search - query
queryan object containing the key/value pairs you want to match
Returns: Array
####Example
> dudes = [{ name: "Jim", age: 8}, { name: "Clive", age: 8}, { name: "Hater", age: 9}]
[ { name: 'Jim', age: 8 },
{ name: 'Clive', age: 8 },
{ name: 'Hater', age: 9 } ]
> a.where(dudes, { age: 8})
[ { name: 'Jim', age: 8 },
{ name: 'Clive', age: 8 } ]
###findWhere(arrayOfObjects, query)
returns the first item from arrayOfObjects where key/value pairs
from query are matched identically
- arrayOfObjects
Arraythe array to search - query
queryan object containing the key/value pairs you want to match
Returns: Object
####Example
> dudes = [{ name: "Jim", age: 8}, { name: "Clive", age: 8}, { name: "Hater", age: 9}]
[ { name: 'Jim', age: 8 },
{ name: 'Clive', age: 8 },
{ name: 'Hater', age: 9 } ]
> a.findWhere(dudes, { age: 8})
{ name: 'Jim', age: 8 }###without(input, toRemove) Returns the input minus the specified values.
- input
Arraythe input array - toRemove
*a single, or array of values to omit
Returns: Array
####Example
> a.without([ 1, 2, 3 ], 2)
[ 1, 3 ]
> a.without([ 1, 2, 3 ], [ 2, 3 ])
[ 1 ]###union(array1, array2, idKey) merge two arrays into a single array of unique values
- array1
ArrayFirst array - array2
ArraySecond array - idKey
stringthe unique ID property name
Returns: Array
####Example
> var array1 = [ 1, 2 ], array2 = [ 2, 3 ];
undefined
> a.union(array1, array2)
[ 1, 2, 3 ]
> var array1 = [ { id: 1 }, { id: 2 } ], array2 = [ { id: 2 }, { id: 3 } ];
undefined
> a.union(array1, array2)
[ { id: 1 }, { id: 2 }, { id: 3 } ]
> var array2 = [ { id: 2, blah: true }, { id: 3 } ]
undefined
> a.union(array1, array2)
[ { id: 1 },
{ id: 2 },
{ id: 2, blah: true },
{ id: 3 } ]
> a.union(array1, array2, "id")
[ { id: 1 }, { id: 2 }, { id: 3 } ]###commonSequence(a, b) Returns the initial elements which both input arrays have in common
- a
Arrayfirst array to compare - b
Arraysecond array to compare
Returns: Array
####Example
> a.commonSequence([1,2,3], [1,2,4])
[ 1, 2 ]###unique(array) reduces an array to unique values
- array
Arrayinput array
Returns: Array
####Example
> n = [1,6,6,7,1]
[ 1, 6, 6, 7, 1 ]
> a.unique(n)
[ 1, 6, 7 ]
###spliceWhile(array, index, test, ...elementN)
splice from index until test fails
- array
Arraythe input array - index
numberthe position to begin splicing from - test
RegExpthe test to continue splicing while true - ...elementN
*the elements to add to the array
Returns: Array
####Example
> letters = ["a", "a", "b"]
[ 'a', 'a', 'b' ]
> a.spliceWhile(letters, 0, /a/, "x")
[ 'a', 'a' ]
> letters
[ 'x', 'b' ]