Methods
arraySum(array) → {number}
- Description:
- Returns sum of all number items in the given array using recursion. Works with any array nesting. Does not calculates not number types.
- Source:
Example
const arr_1 = [1, 2, 3];
console.log(arraySum(arr_1));
// => 6
const arr_2 = ['hello', true, null, [10, [-5, 4]]];
console.log(arraySum(arr_2));
// => 9
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | Array to sum items |
Returns:
Sum of all items in the given array.
- Type
- number
bubbleSort(array) → {Array}
- Description:
- Sorts the array using bubble sort O(n²). Mutates the original array. Do not recommend use this algorightm in the projects apart from educational ones.
- Source:
Example
const arr = [5, 3, 1, 9, 0];
console.log(bubbleSort(arr));
// => [0, 1, 3, 5, 9]
console.log(arr);
// => [0, 1, 3, 5, 9]
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | Array to sort. |
Returns:
Ascending sorted array.
- Type
- Array
chunk(array, sizeopt) → {Array}
- Description:
- Creates an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.
- Source:
Example
console.log(chunk(['a', 'b', 'c', 'd'], 2));
// => [ [ 'a', 'b' ], [ 'c', 'd' ] ]
console.log(chunk(['a', 'b', 'c', 'd'], 3));
// => [ [ 'a', 'b', 'c' ], [ 'd' ] ]
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
array |
Array | The array to chunk. | ||
size |
number |
<optional> |
1
|
The length of each chunk. |
Returns:
Returns the new chunked array.
- Type
- Array
cloneDeep(object) → {Object}
- Description:
- Recursively clones the given object and returns a deep copy of the object.
- Source:
Example
const object_1 = {
a: 1,
b: 2,
c: {
d: {
e: 4,
},
},
};
const object_2 = cloneDeep(object_1);
console.log(object_1 === object_2)
// => false
console.log(object_1.c.d === object_2.c.d);
// => false
Parameters:
Name | Type | Description |
---|---|---|
object |
Object | Object to copy |
Returns:
Returns the deep clone of the given object
- Type
- Object
concat(array, …itemsopt) → {Array}
Example
const array_1 = [1, 2, 3];
const array_2 = concat(array_1, 4, 5, 6, [7, 8, [9]])
console.log(array_2);
// => [1, 2, 3, 4, 5, 6, 7, 8, [9]]
console.log(array_1);
// => [1, 2, 3]
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
array |
Array | The array to concatenate. | |
items |
any |
<optional> <repeatable> |
The values to concatenate. |
Returns:
Returns the new concatenated array.
- Type
- Array
countLetters(string) → {Object}
- Description:
- Create an object that includes count of all letters in the given string. Does not counts spaces.
- Source:
Example
const string = 'Hello wonderful world!';
console.log(countLetters(string));
// => { H: 1, e: 2, l: 4, o: 3, w: 2, n: 1, d: 2, r: 2, f: 1, u: 1, '!': 1 }
Parameters:
Name | Type | Description |
---|---|---|
string |
string | String to count letters. |
Returns:
Object with values of letters.
- Type
- Object
diff(array_1, array_2) → {Array}
- Description:
- Creates an array of array values not included in the other given arrays.
- Source:
Example
console.log(diff([1, 2, 3], [3, 4, 5]));
// => [1, 2]
Parameters:
Name | Type | Description |
---|---|---|
array_1 |
Array | First array. |
array_2 |
Array | Second array. |
Returns:
Returns the new array of filtered values.
- Type
- Array
filter(array, callback) → {Array}
- Description:
- Returns the elements of an array that meet the condition specified in a callback function.
- Source:
Example
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 10];
console.log(filter(numbers, item => item % 2 === 0));
// => [2, 4, 6, 8, 10]
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | array to filter. |
callback |
function | callback function calling for each item in the array. Syntax: currentValue[, index[, array]] |
Returns:
filtered array.
- Type
- Array
flattenDeep(array) → {Array}
- Description:
- Recursively flattens the given array and returns one-dimensional array (or single dimension array).
- Source:
Example
const a = [1, 2, [3, 4], 5, [[[[[6]]],],]];
console.log(flatten(a));
// => [ 1, 2, 3, 4, 5, 6 ]
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | Array to flat. |
Returns:
One-dimensional
- Type
- Array
fromPairs(pairs) → {Object}
Example
const person = [['name', 'Steve'], ['age', 32], ['isAdmin', false]];
console.log(fromPairs(person));
// => { name: 'Steve', age: 32, isAdmin: false }
Parameters:
Name | Type | Description |
---|---|---|
pairs |
Array | Array of arrays, including pairs. |
Returns:
Object created by the given array.
- Type
- Object
getDistance2D(point_1, point_2) → {number}
Example
console.log(getDistance2D([6, 5], [6, 6]));
// => 1
console.log(getDistance2D([0, 0], [10, 0]))
// => 10
Parameters:
Name | Type | Description |
---|---|---|
point_1 |
Array.<number> | The first point. |
point_2 |
Array.<number> | The second point. |
Returns:
distance
- Type
- number
getDistance3D(point_1, point_2) → {number}
Example
console.log(getDistance3D([0, -3, 3], [3, 1, 3]));
// => 5
Parameters:
Name | Type | Description |
---|---|---|
point_1 |
Array.<number> | The first point. |
point_2 |
Array.<number> | The second point. |
Returns:
Distance.
- Type
- number
includes(array, searchElement, fromIndexopt) → {boolean}
- Description:
- Check if the array includes the search element and returns boolean value. Correctly works with NaN using Object.is() method.
- Source:
Example
includes(['apple', 'banana', 'pear'], 'pear')
// => true
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
array |
Array | Array for searching. | ||
searchElement |
any | Element to search. | ||
fromIndex |
number |
<optional> |
0
|
The index where the search starts from. |
Returns:
Result of the search.
- Type
- boolean
intersection(array_1, array_2) → {Array}
- Description:
- Creates an array of unique values that are includes in two given arrays.
- Source:
Example
console.log(intersection([1, 2, 3, 4], [3, 4, 5, 6]));
// => [3, 4]
Parameters:
Name | Type | Description |
---|---|---|
array_1 |
Array | First array. |
array_2 |
Array | Second array. |
Returns:
Returns the new array of intersecting values.
- Type
- Array
map(array, callback) → {Array}
- Description:
- Calls a defined callback function on each element of an array, and returns an array that contains the results.
- Source:
Example
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 10];
console.log(map(numbers, item => item * 2));
// => [
2, 4, 6, 8, 10,
12, 14, 16, 20
]
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | Array to call callback function. |
callback |
function | Function that calling for each item in the array and returns true/false. Syntax: currentValue[, index[, array]] |
Returns:
Mapped array.
- Type
- Array
pop(array) → {any}
- Description:
- Deletes the last element in the array and return deleted element
- Source:
Example
const arr = [1, 2, 3, 4, 5];
console.log(pop(arr));
// => 5;
console.log(arr);
// => [1, 2, 3, 4]
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | array |
Returns:
deleted element
- Type
- any
push(array, …elements) → {number}
- Description:
- Add the elements in the end of given array and returns its new length.
- Source:
Example
const fruits = ['apple', 'banana', 'pear'];
console.log(push(fruits, 'grape', 'kiwi'));
// => 5
console.log(fruits);
// => ['apple', 'banana', 'pear', 'grape', 'kiwi']
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
array |
Array | Array to add elements. | |
elements |
any |
<repeatable> |
To add in the end of array. |
Returns:
New length.
- Type
- number
shift(array) → {any}
Example
const arr = [true, false, 0, 5];
console.log(shift(arr));
// => true
console.log(arr);
// => [false, 0, 5]
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | Array to delete the first item. |
Returns:
Deleted item to the beginning of the array.
- Type
- any
shuffleArray(array) → {Array}
- Description:
- Shuffles the given array and returns it. Uses Fisher-Yates (aka Knuth) Shuffle. Mutates the original array. For mor details: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
- Source:
Example
const array = [0, 1, 2, 3, 4, 5];
console.log(shuffleArray(array));
// => [
3, 8, 6, 2, 5,
1, 7, 0, 4, 9
]
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | Array to shuffle. |
Returns:
Shuffled array
- Type
- Array
unshift(array, …elements) → {number}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
array |
Array | Array to add. | |
elements |
any |
<repeatable> |
To add to the begginning of the array. |
Returns:
New length of the array.
- Type
- number