The array namespace of Bijou.js
Methods
# static exports.arrayDiff(a1, a2) → {Array|String}
Returns the difference between two arrays or strings.
Parameters:
| Name | Type | Description |
|---|---|---|
a1 |
Array
|
String
|
The first array or string |
a2 |
Array
|
String
|
The 2nd array or string. |
The difference between two arrays or strings.
Array
|
String
Example
console.log(_$.arrayDiff(['a', 'b'], ['a', 'b', 'c', 'd'])); // ["c", "d"]
# static exports.averageBy(arr, fn) → {Number}
averageBy
Parameters:
| Name | Type | Description |
|---|---|---|
arr |
Array.<number>
|
The array to average |
fn |
averageByFn
|
The function to apply to each item of the array. |
The average of the array.
Number
Example
Logs the average of the first 4 square numbers:
console.log(_$.averageBy([1,2,3,4], (v) => v ** 2)); // 7.5
# static exports.contains(array, item) → {Boolean}
Returns whether the specified array or string contains the item given.
Parameters:
| Name | Type | Description |
|---|---|---|
array |
Array
|
The array to test with. |
item |
String
|
The item to see if the array contains. |
True or false depending on if the array contains that item.
Boolean
Example
console.log(_$.contains([1,2,3,4,5], 3)); // true
# static exports.count(arr) → {Object}
Counts the items in an array, returning a separate count for each object.
Parameters:
| Name | Type | Description |
|---|---|---|
arr |
Array
|
The array to count items in. |
Object
Example
_$.count(['a', 'b', 'c', 'd', 'e', 'f'])//{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1}
//But if you have multiple items:
_$.count(['a', 'a', b', 'b', 'c', 'd', 'e']);//{'a': 2, 'b': 2, 'c': 1, 'd': 1, 'e': 1}
# static exports.diff(text1, text2) → {Array.<Array.<number>>}
Gets the difference between two strings.
Parameters:
| Name | Type | Description |
|---|---|---|
text1 |
String
|
The 1st text to compare |
text2 |
String
|
The 2nd text to compare with the 1st one. |
An array of arrays, each array in the main array contains 2 numbers, the start and then end of the difference.
Array.<Array.<number>>
Example
console.log(_$.diff("hello earthlings", "hello world"); // [[6,8],[9,16]]
# static exports.each(array, callback) → {Array.<any>}
For each item in an array, run a callback with it.
Parameters:
| Name | Type | Description |
|---|---|---|
array |
Array
|
String
|
Number
|
The array, string or number to run the callback with. |
callback |
eachCallback
|
The callback function to run on the array items. |
The array passed at the beginning.
Array.<any>
Example
_$.each(new Array(6), (array_item, i) => console.log(i));
// 0
// 1
// 2
// 3
// 4
// 5
# static exports.flatten(array, levelopt) → {Array}
Flattens an array level times.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
array |
Array
|
The array to flatten. |
||
level |
Number
|
<optional> |
1 | The number of iterations to flatten it. |
The flattened array.
Array
Example
console.log(_$.flatten(['a', 'b', ['c', 'd']])); // ['a', 'b', 'c', 'd'];
# static exports.nFlatten(arr) → {Array}
Flattens an array recursively.
Parameters:
| Name | Type | Description |
|---|---|---|
arr |
Array
|
The array to flatten. |
The flattened array.
Array
Example
console.log(_$.nFlatten([5,[[9,4],0],[7,6]])); // [5,9,4,0,6,7]
# static exports.remove(array, item)
Removes an item from the array specified.
Parameters:
| Name | Type | Description |
|---|---|---|
array |
Array
|
The array to remove the item from. |
item |
*
|
The item to remove. |
Example
console.log(_$.remove([5, 4, 3, 2, 1], 4)); // [5, 3, 2, 1]
# static exports.shuffleArray(array) → {Array}
Shuffles an array
Parameters:
| Name | Type | Description |
|---|---|---|
array |
Array
|
The array to shuffle. |
The shuffled array.
Array
Example
let array = [1,2,3,4,5];
console.log(_$.shuffleArray(array)); // e.g. [2,4,1,5,3]
# static exports.splice(array, index, item, removeopt) → {String|Array}
Splice but also for strings
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
array |
String
|
Array
|
The array or string to operate on |
||
index |
Number
|
The index to splice |
||
item |
*
|
The item |
||
remove |
Number
|
<optional> |
0 | How many to remove. |
the spliced array or string
String
|
Array
Example
console.log(_$.splice("hello earthlings", 5, " puny")); // "hello puny earthlings"
# static exports.spliceArrayBuffer(arr, start, end, endianopt) → {Number}
Splices an ArrayBuffer.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
arr |
ArrayBuffer
|
Buffer
|
The ArrayBuffer to splice. |
||
start |
Number
|
The start index. |
||
end |
Number
|
The end index. |
||
endian |
Boolean
|
<optional> |
false | Whether to use big endian or not. |
The hex representation of part of the ArrayBuffer.
Number
# static exports.unionArrays(x, y) → {Array}
Joins two arrays together and removes duplicate items.
Parameters:
| Name | Type | Description |
|---|---|---|
x |
Array
|
The first array to join. |
y |
Array
|
The second array to join. |
The joined array from the two other arrays.
Array
Example
console.log(_$.unionArrays([1,2,3,4], [4,5,6])); // [1,2,3,4,5,6]
# static exports.uniqueArray(array) → {Array}
Removes duplicates from an array
Parameters:
| Name | Type | Description |
|---|---|---|
array |
Array
|
The array to remove duplicates from. |
The array with no duplicates.
Array
Example
console.log(_$.uniqueArray([1,1,2,3,4,4,4,5,6)); // [1,2,3,4,5,6]