A package that extends JavaScript arrays with additional useful methods.
npm install array-custom-methods
require('array-custom-methods');
A custom implementation of map
that applies a callback to each element.
Example:
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.myMap(num => num * 2);
console.log(doubled); // Output: [2, 4, 6, 8, 10]
A custom implementation of forEach
that executes a callback for each element.
Example:
const fruits = ['apple', 'banana', 'cherry'];
fruits.myForEach((fruit, index) => {
console.log(`${index}: ${fruit}`);
});
A custom implementation of filter
that returns elements that pass the test.
Example:
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.myFilter(num => num % 2 === 0);
console.log(evenNumbers); // Output: [2, 4]
A custom implementation of reduce
that accumulates values.
Example:
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.myReduce((acc, curr) => acc + curr, 0);
console.log(sum); // Output: 15
Reverses the array without modifying the original.
Example:
const numbers = [1, 2, 3, 4, 5];
const reversed = numbers.revArray();
console.log(reversed); // Output: [5, 4, 3, 2, 1]
A custom implementation of push
.
Example:
const fruits = ['apple', 'banana'];
const newLength = fruits.myPush('cherry', 'date');
console.log(fruits); // Output: ['apple', 'banana', 'cherry', 'date']
console.log(newLength); // Output: 4
Returns the sum of numeric elements.
Example:
const numbers = [10, 20, 30];
console.log(numbers.sum()); // Output: 60
Returns the product of numeric elements.
Example:
const numbers = [2, 3, 4];
console.log(numbers.prod()); // Output: 24
Returns the average of numeric elements.
Example:
const numbers = [10, 20, 30];
console.log(numbers.average()); // Output: 20
Removes duplicate elements.
Example:
const numbers = [1, 2, 2, 3, 4, 4, 5];
console.log(numbers.removeDuplicates()); // Output: [1, 2, 3, 4, 5]
Transforms the array into a Map.
Example:
const fruits = ['apple', 'banana', 'cherry'];
console.log(fruits.hashMapOfArray()); // Output: Map { 0 => 'apple', 1 => 'banana', 2 => 'cherry' }