10 utility functions with no dependencies, to borrow and use for both Node.js and Browser applications to work with Array. Each function has a snippet block and executable demo with a unit tests.
chunk
Split an array into chunks. If array can't be split equally based on the given size, the last chunk will be the remaining elements.
/*
* chunk
* @param {Array} array - List of elements
* @param {Number} size - Length of each chunk to group
* @return {Array} Returns list of grouped chunks
*/functionchunk(array,size=1){return[array.slice(0,size)].concat(chunk(array.slice(size),size));}
Compact is a falsy bouncer that returns a new copy with
all falsey values: false, null, 0, "", undefined, and NaN removed.
/*
* compact
* @param {Array} array - A list of elements to compact
* @return {Array} Returns a filtered values
*/functioncompact(array){returnarray.filter(Boolean);}
Merges any additional values (args) to the first given input/array.
/*
* merge
* @param {Array} array - Main list to link to
* @param {...*} args - The values to chain
* @return {Array} Returns a series or chainable values
*/functionmerge(array,...args){return[...array,...args.flat()]:;}
Demo
⚠️ please select a higher Node version (10+) before you execute the code below
Creates a duplicate-free version from the given input-array
/*
* uniq
* @param {Array} array - List of elements
* @param {Boolean} [sort=false] - optional flag to sort
* @return {Array} Returns uniq values list
*/functionuniq(array,sort=false){returnsort?[...newSet(array)].sort():[...newSet(array)];}
Finds all values that are the intersection/included in all the given arrays/args and creates a list from the result.
/*
* intersection
* @param {...*} args - List of arrays
* @return {Array} Returns a list of unique values
*/functionintersection(...args){const[first,...rest]=args;returnfirst.filter(item=>rest.flat().includes(item));}
Demo
⚠️ please select a higher Node version (10+) before you execute the code below
Creates a list of values from array that are not present in other arrays/args. Result are determined by the first input
/*
* diff
* @param {...*} args - List of arrays
* @return {Array} Returns result of excluded values
*/functiondiff(...args){const[first,...rest]=args;returnfirst.filter(item=>!rest.flat().includes(item));}
Demo
⚠️ please select a higher Node version (10+) before you execute the code below
Creates a copy without the first element in array by destructuring all elements except the first one. If the given list has only one item it returns an empty array []
/*
* allButFirst
* @param {Array} array - List of elements
* @return {Array} Returns filtered list
*/functionallButFirst([,...rest]){returnrest;}
Some of these functions are inspired by popular libraries like underscore and lodash which I highly recommend to use for more complex operations and working with data types as well.
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)