Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

array-tools

Package Overview
Dependencies
Maintainers
1
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-tools

Useful functions for working with arrays

  • 1.4.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.9K
decreased by-43.69%
Maintainers
1
Weekly downloads
 
Created
Source

view on npm npm module downloads per month Build Status Dependency Status

#array-tools Useful functions for working with arrays

Example

var a = require("array-tools");

##a.without(input, toRemove) ⇒ Array Returns the input minus the specified values.

ParamTypeDescription
inputArraythe input array
toRemove*a single, or array of values to omit

Example

> a.without([ 1, 2, 3 ], 2)
[ 1, 3 ]
> a.without([ 1, 2, 3 ], [ 2, 3 ])
[ 1 ]

##a.pluck(arrayOfObjects, ...property) ⇒ Array Plucks the value of the specified property from each object in the input array

ParamTypeDescription
arrayOfObjectsArray.<object>the input array of objects
...propertystringthe property(s) to pluck

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' ]

##a.arrayify(input) ⇒ Array 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 array
  • converts null or undefined to an empty array
ParamTypeDescription
input*the input value to convert to an 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 ]

##a.exists(array, value) ⇒ boolean returns true if a value, or nested object value exists in an array

ParamTypeDescription
arrayArraythe array to search
value*the value to search for

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

##a.where(arrayOfObjects, query) ⇒ Array returns an array containing items from arrayOfObjects where key/value pairs from query are matched identically

ParamTypeDescription
arrayOfObjectsArraythe array to search
queryqueryan object containing the key/value pairs you want to match

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 } ]

##a.findWhere(arrayOfObjects, query) ⇒ object returns the first item from arrayOfObjects where key/value pairs from query are matched identically

ParamTypeDescription
arrayOfObjectsArraythe array to search
queryqueryan object containing the key/value pairs you want to match

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 }

##a.pick(arrayOfObjects, ...property) ⇒ Array.<object> return a copy of the input arrayOfObjects containing objects having only the cherry-picked properties

ParamTypeDescription
arrayOfObjectsArray.<object>the input
...propertystringthe properties to include in the result

Example

> data = [
    { one: "un", two: "deux", three: "trois" },
    { two: "two", one: "one" },
    { four: "quattro" },
    { two: "zwei" }
]
> a.pick(data, "two")
[ { two: 'deux' },
  { two: 'two' },
  { two: 'zwei' } ]

##a.union(array1, array2, idKey) ⇒ Array merge two arrays into a single array of unique values

ParamTypeDescription
array1ArrayFirst array
array2ArraySecond array
idKeystringthe unique ID property name

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 } ]

##a.commonSequence(a, b) ⇒ Array Returns the initial elements which both input arrays have in common

ParamTypeDescription
aArrayfirst array to compare
bArraysecond array to compare

Example

> a.commonSequence([1,2,3], [1,2,4])
[ 1, 2 ]

##a.unique(array) ⇒ Array reduces an array to unique values

ParamTypeDescription
arrayArrayinput array

Example

> n = [1,6,6,7,1]
[ 1, 6, 6, 7, 1 ]
> a.unique(n)
[ 1, 6, 7 ]

##a.spliceWhile(array, index, test, ...elementN) ⇒ Array splice from index until test fails

ParamTypeDescription
arrayArraythe input array
indexnumberthe position to begin splicing from
testRegExpthe test to continue splicing while true
...elementN*the elements to add to the array

Example

> letters = ["a", "a", "b"]
[ 'a', 'a', 'b' ]
> a.spliceWhile(letters, 0, /a/, "x")
[ 'a', 'a' ]
> letters
[ 'x', 'b' ]

##a.extract(array, query) ⇒ Array Removes items from array which satisfy the query. Modifies the input array, returns the extracted.

ParamTypeDescription
arrayArraythe input array, modified directly
queryfunction | objectPer item in the array, if either the function returns truthy or the exists query is satisfied, the item is extracted

Returns: Array - the extracted items.
##a.flatten() ⇒ Array flatten an array of arrays into a single array

Since: 1.4.0
Todo

  • document

Example

> numbers = [ 1, 2, [ 3, 4 ], 5 ]
[ 1,
  2,
  [ 3, 4 ],
  5 ]
> a.flatten(numbers)
[ 1, 2, 3, 4, 5 ]

documented by jsdoc-to-markdown.

Keywords

FAQs

Package last updated on 22 Dec 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc