Created
January 26, 2024 11:04
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: