Array extension.
Array extension to create new array elements, iterate the collection and return the array itself and methods to return a single value.
Get started.
Installation
This guide will help you to install the array extension in your project.
Firstly, you need to go to the project path and open a new terminal and do the next (choose the best option for you):
npm package.
As a globalnpm i array-extension-js -g
npm package.
As a localnpm i --save array-extension-js
Then, you're going to add the next code in your index file:
require('array-extension-js')
And...your project is ready to use this extension!
Features
Array methods
each(callback).
This method iterates over the array elements and executes the callback function.
Sintax: array.each(callback)
Params: callback
function that sends two arguments: item
and index
.
Implementation example: each.js
where(spec).
This method creates a new array that contains all the elements that satisfies the given specification.
Sintax: array.where(spec)
Params: spec
is a callback function that will decide if the element shall or not to be included in the new list.
Implementation example: where.js
any(spec).
This method shall return a true value if any of the elements in the array satisfies the given spec.
Sintax: array.any(spec)
Params: spec
is a function then it shall be called for each element in the array. If the spec
parameter is not a function object, then it will be tested against each element of the array.
Implementation example: any.js
select(spec).
Creates a new collection containing the elements returned by the spec function. It allows to make an excerpt of the collection, it would be useful to hide information.
Syntax: array.select(spec)
Params:spec
is a callback function that takes element
& index
as arguments, and returns the desired part of the element to form a new array.
Implementation example: select.js
take(howMany, spec).
This method returns a new array containing ideally howMany
elements.
Syntax: array.take(howMany, spec)
or array.take(howMany)
Params: spec
argument defines the criteria, this method will use to fill up the new array (if there is no spec
argument then it shall take just the first howMany
elements in the collection).
Implementation example: take.js
skip(howMany).
This method produces a new Array which will not include the first howMany elements.
Syntax: array.skip(howMany)
Params: howMany
argument defines the first elements to not include in the new array.
Implementation example: skip.js
first(spec).
This method returns the first element on collection that satisfies the specification, if there is no specification then it will return the very first array’s element.
Syntax: array.first(spec)
Params: spec
argument defines the criteria to get an element.
Implementation example: first.js
last(spec).
This method returns the last collection’s element that satisfies the given specification, if there is no specification then will return just the last element.
Syntax: array.last(spec)
Params: spec
argument defines the criteria to get an element.
Implementation example: last.js
count(spec).
This method returns the number of elements on the collection that satisfies the specification, if no specification is present then it will return just the array’s length property.
Syntax: array.count(spec)
Params: spec
argument defines the criteria to count an element.
Implementation example: count.js
index(spec).
This method returns the zero based position in the array of the element that satisfies the specification.
Syntax: array.index(spec)
Params: spec
argument defines the criteria to get the element index.
Implementation example: index.js
pluck(property)
This method retrieves the given property of each element in the array and return a new array containing that property values.
Syntax: array.pluck(property)
Params: property
argument defines the property to get elements.
Implementation example: pluck.js
sum(spec).
This method returns the summatory of the result of executing the spec function on each array’s element.
Syntax: array.sum(spec)
Params: spec
argument is the criteria to do the summatory, if there is not spec function then it will return the summatory of the array’s elements.
Implementation example: sum.js
max(comparer).
This method finds and return the maximum value on the collection.
Syntax: array.max(comparer)
Params: comparer
argument should be a function that receives 2 parameters, valueA and valueB and shall return a negative number for valueA < valueB scenario, zero when valueA === valueB and finally a positive number when valueA > valueB. If not comparer is specified then it will try to evaluate the array elements as if they are numbers.
Implementation example: max.js
min(comparer).
This method finds and return the smallest value on the collection.
Syntax: array.min(comparer)
Params: comparer
argument should be a function that receives 2 parameters, valueA and valueB and shall return a negative number for valueA < valueB scenario, zero when valueA === valueB and finally a positive number when valueA > valueB. If not comparer is specified then it will try to evaluate the array elements as if they are numbers.
Implementation example: min.js
flatten().
This method returns a new flat array.
Syntax: array.flatten()
Implementation example: flatten.js