array-extended
array-extended
is a Javascript library that can be used standalone or incorporated into extended
var array = require("array-extended");
Or
var myextended = require("extended")
.register(require("array-extended"));
Installation
npm install array-extended
Or download the source (minified)
Usage
array-extended
includes many useful methods that for array. All of the functions can be chained together using array-exteded
as a function or you can use the functions directly as shown in the examples.
The following native ES5
methods are included for completeness.
forEach
map
filter
reduce
reduceRight
some
every
indexOf
lastIndexOf
Note forEach
returns the original array for chaining purposes.
sum
Sums the values of an array.
array.sum([1,2,3]);
array([1,2,3]).sum().value();
avg
Finds the average of an array of numbers.
array.avg([1,2,3]);
array([1,2,3]).avg().value();
sort
Sorts an array based on a property, by natural ordering, or by a custom comparator.
Note this does not change the original array.
array.sort([{a: 1},{a: 2},{a: -2}], "a");
array([{a: 1},{a: 2},{a: -2}]).sort("a");
min
Finds the minimum value in an array based on a property, by natural ordering, or by a custom comparator.
array.min([ 3, -3, -2, -1, 1, 2]);
array.min([{a: 1},{a: 2},{a: -2}], "a");
array([ 3, -3, -2, -1, 1, 2]).min();
array([{a: 1},{a: 2},{a: -2}]).min("a");
max
Finds the maximum value in an array based on a property, by natural ordering, or by a custom comparator.
array.max([ 3, -3, -2, -1, 1, 2]);
array.max([{a: 1},{a: 2},{a: -2}], "a");
array([ 3, -3, -2, -1, 1, 2]).max();
array([{a: 1},{a: 2},{a: -2}]).max("a");
difference
Finds the difference between two arrays.
array.difference([1, 2, 3], [2]);
array.difference([true, false], [false]);
array.difference(["a", "b", 3], [3]);
var a = , b = , c = ;
array.difference([{a: 1}, {a: 2}, {a: 3}], [{a: 2}, {a: 3}]);
array([true, false]).difference([false]).value()
array([1, 2, 3]).difference([2]).value()
array([1, 2, 3]).difference([2], [3]).value();
array(["a", "b", 3]).difference([3]).value(); ["a", "b"]
array([{a: 1}, {a: 2}, {a: 3}]).difference([{a: 2}, {a: 3}]).value(); [{a: 1}]
unique
Removed duplicate values from an array.
array.unique([1, 2, 2, 3, 3, 3, 4, 4, 4]);
array([1, 2, 2, 3, 3, 3, 4, 4, 4]).unique().value();
array(["a", "b", "b"]).unique().value();
array.unique(["a", "b", "b"]);
rotate
Rotates an array by the number of places for 1 position by default.
var arr = array(["a", "b", "c", "d"])
arr.rotate().value();
arr.rotate(2).value();
arr.rotate(3).value();
arr.rotate(4).value();
arr.rotate(-1).value();
arr.rotate(-2).value();
arr.rotate(-3).value();
arr.rotate(-4).value();
var arr = ["a", "b", "c", "d"];
array.rotate(arr);
array.rotate(arr, 2);
array.rotate(arr, 3);
array.rotate(arr, 4);
array.rotate(arr, -1)
array.rotate(arr, -2);
array.rotate(arr, -3);
array.rotate(arr, -4);
permutations
Finds all permutations of an array.
array([1, 2, 3]).permutations();
array([1, 2, 3]).permutations(2);
array.permutations([1, 2, 3]);
array.permutations(([1, 2, 3], 2);
zip
Zips the values of multiple arrays into a single array.
array([1]).zip([2], [3]).value();
array([1, 2]).zip([2], [3]).value();
array([1, 2, 3]).zip([ 4, 5, 6 ], b).value();
array([1, 2]).zip([ 4, 5, 6 ], [7, 8, 9 ]).value();
array([ 4, 5, 6 ]).zip([1, 2], [8]).value(),
array.zip([1], [2], [3]);
array.zip([1, 2], [2], [3]);
array.zip([1, 2, 3], [4,5,6], [7, 8, 9 ]);
array.zip([1, 2], [4,5,6], [7, 8, 9 ]);
array.zip([ 4, 5, 6 ], [1, 2], [8]);
transpose
Transpose a multi dimensional array.
array([[1, 2, 3],[4, 5, 6]]).transpose().value();
array([[1, 2],[3, 4],[5, 6]]).transpose().value();
array([[1],[3, 4],[5, 6]]).transpose().value();
array.transpose([[1, 2, 3],[4, 5, 6]]);
array.transpose([[1, 2],[3, 4],[5, 6]]);
array.transpose([[1],[3, 4],[5, 6]]);
valuesAt
Gathers values and the specified indexes.
var arr = array(["a", "b", "c", "d"]);
arr.valuesAt(1, 2, 3).value();
arr.valuesAt(1, 2, 3, 4).value();
arr.valuesAt(0, 3).value();
arr = ["a", "b", "c", "d"];
array.valuesAt(arr, 1, 2, 3);
array.valuesAt(arr, 1, 2, 3, 4);
array.valuesAt(arr, 0, 3);
union
Finds the union of two arrays.
array(["a", "b", "c"]).union(["b", "c", "d"]).value();
array(["a"]).union(["b"], ["c"], ["d"], ["c"]).value();
array.union(["a", "b", "c"], ["b", "c", "d"]);
array.union(["a"], ["b"], ["c"], ["d"], ["c"]);
intersect
Finds the intersection of two arrays.
array([1, 2]).intersect([2, 3], [2, 3, 5]).value();
array([1, 2, 3]).intersect([2, 3, 4, 5], [2, 3, 5]).value();
array([1, 2, 3, 4]).intersect([2, 3, 4, 5], [2, 3, 4, 5]).value();
array([1, 2, 3, 4, 5]).intersect([1, 2, 3, 4, 5], [1, 2, 3]).value();
array([[1, 2, 3, 4, 5],[1, 2, 3, 4, 5],[1, 2, 3]]).intersect().value();
array.intersect([1, 2], [2, 3], [2, 3, 5]);
array.intersect([1, 2, 3], [2, 3, 4, 5], [2, 3, 5]);
array.intersect([1, 2, 3, 4], [2, 3, 4, 5], [2, 3, 4, 5]);
array.intersect([1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3]);
array.intersect([[1, 2, 3, 4, 5],[1, 2, 3, 4, 5], [1, 2, 3]]);
powerSet
Finds the powerset of a given array.
array([1, 2, 3]).powerSet().value();
array.powerSet([1, 2, 3]);
cartesian
Finds the cartesian product of arrays.
array([1, 2]).cartesian([2, 3]).value();
array.cartesian([1, 2], [2, 3]);
compact
Compacts the values of an array.
array([1, null, null, x, 2]).compact().value();
array([1, 2]).compact().value();
array.compact([1, null, null, x, 2]);
array.compact([1, 2]);
multiply
Repoduces the values in an array the given number of times.
array([1, 2]).multiply(2).value();
array.multiply([1, 2, 3], 2);
flatten
Flatten multiple arrays into a single array.
array([ [1], [2], [3] ]).flatten().value();
array.flatten([1, 2], [2, 3], [3, 4]);
pluck
Pluck properties from values in an array.
var arr = [
{name: {first: "Fred", last: "Jones"}, age: 50, roles: ["a", "b", "c"]},
{name: {first: "Bob", last: "Yukon"}, age: 40, roles: ["b", "c"]},
{name: {first: "Alice", last: "Palace"}, age: 35, roles: ["c"]},
{name: {first: "Johnny", last: "P."}, age: 56, roles: []}
];
array.pluck(arr, "name.first");
array(arr).pluck("age");
invoke
Invokes the specified method on each value in an array.
function person(name, age) {
return {
getName: function () {
return name;
},
getOlder: function () {
age++;
return this;
},
getAge: function () {
return age;
}
};
};
var arr = [person("Bob", 40), person("Alice", 35), person("Fred", 50), person("Johnny", 56)];
array.invoke(arr, "getName");
array(arr).invoke("getName").value();
array(arr).invoke("getOlder").invoke("getAge").value();