Skip to content

Instantly share code, notes, and snippets.

@baliyan9887
Created January 26, 2024 11:04
Show Gist options
  • Save baliyan9887/e4c8bd26255698f25764d2dd706d8321 to your computer and use it in GitHub Desktop.
Save baliyan9887/e4c8bd26255698f25764d2dd706d8321 to your computer and use it in GitHub Desktop.
A collection of generic functions for common array manipulations in JavaScript. These functions provide utility for tasks like pushing, popping, sorting, filtering, mapping, and more.
// 1. Push an Item to the Array
function pushItem(arr, item) {
arr.push(item);
}
// 2. Pop an Item from the Array
function popItem(arr) {
arr.pop();
}
// 3. Unshift an Item to the Array
function unshiftItem(arr, item) {
arr.unshift(item);
}
// 4. Shift an Item from the Array
function shiftItem(arr) {
arr.shift();
}
// 5. Insert an Item at a Specific Index
function insertItemAt(arr, index, item) {
arr.splice(index, 0, item);
}
// 6. Remove an Item at a Specific Index
function removeItemAt(arr, index) {
arr.splice(index, 1);
}
// 7. Update an Item at a Specific Index
function updateItemAt(arr, index, newItem) {
arr[index] = newItem;
}
// 8. Reverse the Array
function reverseArray(arr) {
return arr.reverse();
}
// 9. Sort the Array
function sortArray(arr, compareFunction) {
return arr.slice().sort(compareFunction);
}
// 10. Filter Items Based on a Condition
function filterItems(arr, condition) {
return arr.filter(condition);
}
// 11. Slice the Array
function sliceArray(arr, start, end) {
return arr.slice(start, end);
}
// 12. Concatenate Arrays
function concatenateArrays(arr1, arr2) {
return [...arr1, ...arr2];
}
// 13. Find an Item in the Array
function findItem(arr, condition) {
return arr.find(condition);
}
// 14. Find Index of an Item
function findIndex(arr, condition) {
return arr.findIndex(condition);
}
// 15. Check if Array Includes an Item
function includesItem(arr, item) {
return arr.includes(item);
}
// 16. Check if All Items Satisfy a Condition
function allItemsSatisfy(arr, condition) {
return arr.every(condition);
}
// 17. Check if Any Item Satisfies a Condition
function anyItemSatisfies(arr, condition) {
return arr.some(condition);
}
// 18. Reduce Array to a Single Value
function reduceArray(arr, reducer, initialValue) {
return arr.reduce(reducer, initialValue);
}
// 19. Remove Duplicates from an Array
function removeDuplicates(arr) {
return [...new Set(arr)];
}
// 20. Clear All Items from the Array
function clearArray(arr) {
return [];
}
// 21. Map over Items and Transform Them
function mapArray(arr, transformation) {
return arr.map(transformation);
}
// 22. Execute a Function for Each Item
function forEachItem(arr, action) {
arr.forEach(action);
}
// 23. Count the Number of Occurrences of an Item
function countOccurrences(arr, item) {
return arr.reduce((count, current) => (current === item ? count + 1 : count), 0);
}
// 24. Split Array into Chunks of a Given Size
function chunkArray(arr, chunkSize) {
const result = [];
for (let i = 0; i < arr.length; i += chunkSize) {
result.push(arr.slice(i, i + chunkSize));
}
return result;
}
// 25. Rotate Array to the Left by a Given Number of Positions
function rotateLeft(arr, positions) {
const rotated = [...arr.slice(positions), ...arr.slice(0, positions)];
return rotated;
}
// 26. Rotate Array to the Right by a Given Number of Positions
function rotateRight(arr, positions) {
const rotated = [...arr.slice(-positions), ...arr.slice(0, -positions)];
return rotated;
}
// 27. Partition Array into Two Arrays Based on a Condition
function partitionArray(arr, condition) {
const trueArray = arr.filter(condition);
const falseArray = arr.filter((item) => !condition(item));
return [trueArray, falseArray];
}
// 28. Shuffle Array Randomly
function shuffleArray(arr) {
const shuffled = [...arr];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
return shuffled;
}
// 29. Find the Maximum Value in an Array
function findMaxValue(arr) {
return Math.max(...arr);
}
// 30. Find the Minimum Value in an Array
function findMinValue(arr) {
return Math.min(...arr);
}
// Add comments for each new function
// Export the updated set of functions
export {
pushItem,
popItem,
unshiftItem,
shiftItem,
insertItemAt,
removeItemAt,
updateItemAt,
reverseArray,
sortArray,
filterItems,
sliceArray,
concatenateArrays,
findItem,
findIndex,
includesItem,
allItemsSatisfy,
anyItemSatisfies,
reduceArray,
removeDuplicates,
clearArray,
mapArray,
forEachItem,
countOccurrences,
chunkArray,
rotateLeft,
rotateRight,
partitionArray,
shuffleArray,
findMaxValue,
findMinValue,
};
// 1. Push an Item to the Array
function pushItem<T>(arr: T[], item: T): void {
arr.push(item);
}
// 2. Pop an Item from the Array
function popItem<T>(arr: T[]): void {
arr.pop();
}
// 3. Unshift an Item to the Array
function unshiftItem<T>(arr: T[], item: T): void {
arr.unshift(item);
}
// 4. Shift an Item from the Array
function shiftItem<T>(arr: T[]): void {
arr.shift();
}
// 5. Insert an Item at a Specific Index
function insertItemAt<T>(arr: T[], index: number, item: T): void {
arr.splice(index, 0, item);
}
// 6. Remove an Item at a Specific Index
function removeItemAt<T>(arr: T[], index: number): void {
arr.splice(index, 1);
}
// 7. Update an Item at a Specific Index
function updateItemAt<T>(arr: T[], index: number, newItem: T): void {
arr[index] = newItem;
}
// 8. Reverse the Array
function reverseArray<T>(arr: T[]): T[] {
return arr.slice().reverse();
}
// 9. Sort the Array
function sortArray<T>(arr: T[], compareFunction?: (a: T, b: T) => number): T[] {
return arr.slice().sort(compareFunction);
}
// 10. Filter Items Based on a Condition
function filterItems<T>(arr: T[], condition: (item: T) => boolean): T[] {
return arr.filter(condition);
}
// 11. Slice the Array
function sliceArray<T>(arr: T[], start: number, end?: number): T[] {
return arr.slice(start, end);
}
// 12. Concatenate Arrays
function concatenateArrays<T>(arr1: T[], arr2: T[]): T[] {
return [...arr1, ...arr2];
}
// 13. Find an Item in the Array
function findItem<T>(arr: T[], condition: (item: T) => boolean): T | undefined {
return arr.find(condition);
}
// 14. Find Index of an Item
function findIndex<T>(arr: T[], condition: (item: T) => boolean): number {
return arr.findIndex(condition);
}
// 15. Check if Array Includes an Item
function includesItem<T>(arr: T[], item: T): boolean {
return arr.includes(item);
}
// 16. Check if All Items Satisfy a Condition
function allItemsSatisfy<T>(arr: T[], condition: (item: T) => boolean): boolean {
return arr.every(condition);
}
// 17. Check if Any Item Satisfies a Condition
function anyItemSatisfies<T>(arr: T[], condition: (item: T) => boolean): boolean {
return arr.some(condition);
}
// 18. Reduce Array to a Single Value
function reduceArray<T, U>(arr: T[], reducer: (accumulator: U, current: T) => U, initialValue: U): U {
return arr.reduce(reducer, initialValue);
}
// 19. Remove Duplicates from an Array
function removeDuplicates<T>(arr: T[]): T[] {
return [...new Set(arr)];
}
// 20. Clear All Items from the Array
function clearArray<T>(arr: T[]): T[] {
return [];
}
// 21. Map over Items and Transform Them
function mapArray<T, U>(arr: T[], transformation: (item: T) => U): U[] {
return arr.map(transformation);
}
// 22. Execute a Function for Each Item
function forEachItem<T>(arr: T[], action: (item: T) => void): void {
arr.forEach(action);
}
// 23. Count the Number of Occurrences of an Item
function countOccurrences<T>(arr: T[], item: T): number {
return arr.reduce((count, current) => (current === item ? count + 1 : count), 0);
}
// 24. Split Array into Chunks of a Given Size
function chunkArray<T>(arr: T[], chunkSize: number): T[][] {
const result: T[][] = [];
for (let i = 0; i < arr.length; i += chunkSize) {
result.push(arr.slice(i, i + chunkSize));
}
return result;
}
// 25. Rotate Array to the Left by a Given Number of Positions
function rotateLeft<T>(arr: T[], positions: number): T[] {
const rotated = [...arr.slice(positions), ...arr.slice(0, positions)];
return rotated;
}
// 26. Rotate Array to the Right by a Given Number of Positions
function rotateRight<T>(arr: T[], positions: number): T[] {
const rotated = [...arr.slice(-positions), ...arr.slice(0, -positions)];
return rotated;
}
// 27. Partition Array into Two Arrays Based on a Condition
function partitionArray<T>(arr: T[], condition: (item: T) => boolean): [T[], T[]] {
const trueArray = arr.filter(condition);
const falseArray = arr.filter((item) => !condition(item));
return [trueArray, falseArray];
}
// 28. Shuffle Array Randomly
function shuffleArray<T>(arr: T[]): T[] {
const shuffled = [...arr];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
return shuffled;
}
// 29. Find the Maximum Value in an Array
function findMaxValue<T extends number>(arr: T[]): T {
return Math.max(...arr);
}
// 30. Find the Minimum Value in an Array
function findMinValue<T extends number>(arr: T[]): T {
return Math.min(...arr);
}
// Add comments for each new function
// Export the updated set of functions
export {
pushItem,
popItem,
unshiftItem,
shiftItem,
insertItemAt,
removeItemAt,
updateItemAt,
reverseArray,
sortArray,
filterItems,
sliceArray,
concatenateArrays,
findItem,
findIndex,
includesItem,
allItemsSatisfy,
anyItemSatisfies,
reduceArray,
removeDuplicates,
clearArray,
mapArray,
forEachItem,
countOccurrences,
chunkArray,
rotateLeft,
rotateRight,
partitionArray,
shuffleArray,
findMaxValue,
findMinValue,
};
@baliyan9887
Copy link
Author

baliyan9887 commented Jan 26, 2024

Array Manipulation Functions

A collection of generic functions for common array manipulations in JavaScript. These functions provide utility for tasks like pushing, popping, sorting, filtering, mapping, and more.

Included Functions:

  1. pushItem: Add an item to the end of the array.
  2. popItem: Remove and return the last item from the array.
  3. unshiftItem: Add an item to the beginning of the array.
  4. shiftItem: Remove and return the first item from the array.
  5. insertItemAt: Insert an item at a specific index in the array.
  6. removeItemAt: Remove an item at a specific index from the array.
  7. updateItemAt: Update the value of an item at a specific index in the array.
  8. reverseArray: Reverse the order of items in the array.
  9. sortArray: Sort the array in ascending order.
  10. filterItems: Filter items based on a given condition.
  11. sliceArray: Extract a portion of the array.
  12. concatenateArrays: Concatenate multiple arrays into one.
  13. findItem: Find the first item satisfying a condition.
  14. findIndex: Find the index of the first item satisfying a condition.
  15. includesItem: Check if the array includes a specific item.
  16. allItemsSatisfy: Check if all items satisfy a condition.
  17. anyItemSatisfies: Check if any item satisfies a condition.
  18. reduceArray: Reduce the array to a single value.
  19. removeDuplicates: Remove duplicate items from the array.
  20. clearArray: Remove all items from the array.
  21. mapArray: Map over items and transform them.
  22. forEachItem: Execute a function for each item.
  23. countOccurrences: Count the number of occurrences of an item.
  24. chunkArray: Split the array into chunks of a given size.
  25. rotateLeft: Rotate the array to the left by a given number of positions.
  26. rotateRight: Rotate the array to the right by a given number of positions.
  27. partitionArray: Partition the array into two arrays based on a condition.
  28. shuffleArray: Shuffle the array randomly.
  29. findMaxValue: Find the maximum value in the array.
  30. findMinValue: Find the minimum value in the array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment