- Spec files
npm-lodash
Describes: pkg:npm/lodash@4.5.x
- Description
- Comprehensive JavaScript utility library with 300+ methods for arrays, objects, strings, functions, and more.
- Author
- tessl
- Last updated
array-methods.md docs/
1# Array Methods23Comprehensive utilities for working with arrays including chunking, flattening, set operations, and element manipulation.45## Capabilities67### Array Chunking & Slicing89#### chunk10Creates an array of elements split into groups of the length of size.1112```javascript { .api }13/**14* Creates an array of elements split into groups of the length of size15* @param array - The array to process16* @param size - The length of each chunk17* @returns Returns the new array of chunks18*/19function chunk(array, size = 1);20```2122#### slice23Creates a slice of array from start up to, but not including, end.2425```javascript { .api }26/**27* Creates a slice of array from start up to, but not including, end28* @param array - The array to slice29* @param start - The start position30* @param end - The end position31* @returns Returns the slice of array32*/33function slice(array, start = 0, end = array.length);34```3536#### take & drop37Extract or remove elements from array beginning/end.3839```javascript { .api }40/**41* Creates a slice of array with n elements taken from the beginning42* @param array - The array to query43* @param n - The number of elements to take44* @returns Returns the slice of array45*/46function take(array, n = 1);4748/**49* Creates a slice of array with n elements dropped from the beginning50* @param array - The array to query51* @param n - The number of elements to drop52* @returns Returns the slice of array53*/54function drop(array, n = 1);5556/**57* Creates a slice of array with n elements taken from the end58* @param array - The array to query59* @param n - The number of elements to take60* @returns Returns the slice of array61*/62function takeRight(array, n = 1);6364/**65* Creates a slice of array with n elements dropped from the end66* @param array - The array to query67* @param n - The number of elements to drop68* @returns Returns the slice of array69*/70function dropRight(array, n = 1);71```7273### Array Cleaning & Filtering7475#### compact76Creates an array with all falsey values removed.7778```javascript { .api }79/**80* Creates an array with all falsey values removed81* @param array - The array to compact82* @returns Returns the new array of filtered values83*/84function compact(array);85```8687#### without88Creates an array excluding all given values.8990```javascript { .api }91/**92* Creates an array excluding all given values using SameValueZero for equality comparisons93* @param array - The array to inspect94* @param values - The values to exclude95* @returns Returns the new array of filtered values96*/97function without(array, ...values);98```99100#### pull & remove101Remove elements from array (mutating or non-mutating).102103```javascript { .api }104/**105* Removes all given values from array using SameValueZero for equality comparisons106* @param array - The array to modify107* @param values - The values to remove108* @returns Returns array109*/110function pull(array, ...values);111112/**113* Removes all elements from array that predicate returns truthy for114* @param array - The array to modify115* @param predicate - The function invoked per iteration116* @returns Returns an array of removed elements117*/118function remove(array, predicate);119```120121### Array Flattening122123#### flatten124Flattens array a single level deep.125126```javascript { .api }127/**128* Flattens array a single level deep129* @param array - The array to flatten130* @returns Returns the new flattened array131*/132function flatten(array);133134/**135* Recursively flattens array136* @param array - The array to flatten137* @returns Returns the new flattened array138*/139function flattenDeep(array);140141/**142* Recursively flattens array up to depth times143* @param array - The array to flatten144* @param depth - The maximum recursion depth145* @returns Returns the new flattened array146*/147function flattenDepth(array, depth = 1);148```149150### Array Set Operations151152#### uniq153Creates a duplicate-free version of an array.154155```javascript { .api }156/**157* Creates a duplicate-free version of an array158* @param array - The array to inspect159* @returns Returns the new duplicate free array160*/161function uniq(array);162163/**164* Like uniq but accepts iteratee for computing uniqueness165* @param array - The array to inspect166* @param iteratee - The iteratee invoked per element167* @returns Returns the new duplicate free array168*/169function uniqBy(array, iteratee);170171/**172* Like uniq but accepts comparator for comparing elements173* @param array - The array to inspect174* @param comparator - The comparator invoked per element175* @returns Returns the new duplicate free array176*/177function uniqWith(array, comparator);178```179180#### difference181Creates an array of unique array values not included in the other given arrays.182183```javascript { .api }184/**185* Creates an array of unique array values not included in the other given arrays186* @param array - The array to inspect187* @param values - The values to exclude188* @returns Returns the new array of filtered values189*/190function difference(array, ...values);191192/**193* Like difference but accepts iteratee for computing differences194* @param array - The array to inspect195* @param values - The values to exclude196* @param iteratee - The iteratee invoked per element197* @returns Returns the new array of filtered values198*/199function differenceBy(array, values, iteratee);200201/**202* Like difference but accepts comparator for comparing elements203* @param array - The array to inspect204* @param values - The values to exclude205* @param comparator - The comparator invoked per element206* @returns Returns the new array of filtered values207*/208function differenceWith(array, values, comparator);209```210211#### intersection212Creates an array of unique values that are included in all given arrays.213214```javascript { .api }215/**216* Creates an array of unique values that are included in all given arrays217* @param arrays - The arrays to inspect218* @returns Returns the new array of intersecting values219*/220function intersection(...arrays);221222/**223* Like intersection but accepts iteratee for computing intersections224* @param arrays - The arrays to inspect (last parameter is iteratee)225* @returns Returns the new array of intersecting values226*/227function intersectionBy(...arrays);228229/**230* Like intersection but accepts comparator for comparing elements231* @param arrays - The arrays to inspect (last parameter is comparator)232* @returns Returns the new array of intersecting values233*/234function intersectionWith(...arrays);235```236237#### union & xor238Union and symmetric difference operations.239240```javascript { .api }241/**242* Creates an array of unique values from all given arrays243* @param arrays - The arrays to inspect244* @returns Returns the new array of combined values245*/246function union(...arrays);247248/**249* Creates an array of unique values that is the symmetric difference of the given arrays250* @param arrays - The arrays to inspect251* @returns Returns the new array of filtered values252*/253function xor(...arrays);254```255256### Array Searching & Indexing257258#### indexOf & findIndex259Find element positions in arrays.260261```javascript { .api }262/**263* Gets the index at which the first occurrence of value is found in array264* @param array - The array to inspect265* @param value - The value to search for266* @param fromIndex - The index to search from267* @returns Returns the index of the matched value, else -1268*/269function indexOf(array, value, fromIndex = 0);270271/**272* Returns the index of the first element predicate returns truthy for273* @param array - The array to inspect274* @param predicate - The function invoked per iteration275* @param fromIndex - The index to search from276* @returns Returns the index of the found element, else -1277*/278function findIndex(array, predicate, fromIndex = 0);279280/**281* Like findIndex but searches from right to left282* @param array - The array to inspect283* @param predicate - The function invoked per iteration284* @param fromIndex - The index to search from285* @returns Returns the index of the found element, else -1286*/287function findLastIndex(array, predicate, fromIndex = array.length - 1);288```289290### Array Transformation & Zipping291292#### zip & unzip293Group corresponding elements from arrays.294295```javascript { .api }296/**297* Creates an array of grouped elements298* @param arrays - The arrays to process299* @returns Returns the new array of grouped elements300*/301function zip(...arrays);302303/**304* Creates an array of grouped elements, the inverse of zip305* @param array - The array of grouped elements to process306* @returns Returns the new array of regrouped elements307*/308function unzip(array);309310/**311* Like zip but accepts iteratee to specify how grouped values should be combined312* @param arrays - The arrays to process (last parameter is iteratee)313* @returns Returns the new array of grouped elements314*/315function zipWith(...arrays);316```317318#### zipObject319Creates an object composed from arrays of property identifiers and values.320321```javascript { .api }322/**323* Creates an object composed from arrays of property identifiers and values324* @param props - The property identifiers325* @param values - The property values326* @returns Returns the new object327*/328function zipObject(props, values);329330/**331* Like zipObject but supports property paths332* @param props - The property identifiers333* @param values - The property values334* @returns Returns the new object335*/336function zipObjectDeep(props, values);337```338339### Array Utilities340341#### fill342Fills elements of array with value.343344```javascript { .api }345/**346* Fills elements of array with value from start up to, but not including, end347* @param array - The array to fill348* @param value - The value to fill array with349* @param start - The start position350* @param end - The end position351* @returns Returns array352*/353function fill(array, value, start = 0, end = array.length);354```355356#### reverse357Reverses array so that the first element becomes the last.358359```javascript { .api }360/**361* Reverses array so that the first element becomes the last362* @param array - The array to modify363* @returns Returns array364*/365function reverse(array);366```367368#### join369Converts all elements in array into a string separated by separator.370371```javascript { .api }372/**373* Converts all elements in array into a string separated by separator374* @param array - The array to convert375* @param separator - The element separator376* @returns Returns the joined string377*/378function join(array, separator = ',');379```380381#### first & last (head & tail)382Get first and last elements.383384```javascript { .api }385/**386* Gets the first element of array387* @param array - The array to query388* @returns Returns the first element of array389*/390function head(array);391392/**393* Gets the last element of array394* @param array - The array to query395* @returns Returns the last element of array396*/397function last(array);398399/**400* Gets all but the first element of array401* @param array - The array to query402* @returns Returns the slice of array403*/404function tail(array);405406/**407* Gets all but the last element of array408* @param array - The array to query409* @returns Returns the slice of array410*/411function initial(array);412```413414### Sorted Array Methods415416#### sortedIndex417Uses binary search to determine the lowest index at which value should be inserted into array.418419```javascript { .api }420/**421* Uses binary search to determine the lowest index at which value should be inserted into array422* @param array - The sorted array to inspect423* @param value - The value to evaluate424* @returns Returns the index at which value should be inserted into array425*/426function sortedIndex(array, value);427428/**429* Like sortedIndex but accepts iteratee for computing sort ranking430* @param array - The sorted array to inspect431* @param value - The value to evaluate432* @param iteratee - The iteratee invoked per element433* @returns Returns the index at which value should be inserted into array434*/435function sortedIndexBy(array, value, iteratee);436```437438#### sortedUniq439Creates a duplicate-free version of an array using SameValueZero for equality comparisons.440441```javascript { .api }442/**443* Creates a duplicate-free version of an array using SameValueZero for equality comparisons444* @param array - The sorted array to inspect445* @returns Returns the new duplicate free array446*/447function sortedUniq(array);448449/**450* Like sortedUniq but accepts iteratee for computing uniqueness451* @param array - The sorted array to inspect452* @param iteratee - The iteratee invoked per element453* @returns Returns the new duplicate free array454*/455function sortedUniqBy(array, iteratee);456```457458### Array Concatenation & Building459460#### concat461Creates a new array concatenating array with any additional arrays and/or values.462463```javascript { .api }464/**465* Creates a new array concatenating array with any additional arrays and/or values466* @param array - The array to concatenate467* @param values - The values to concatenate468* @returns Returns the new concatenated array469*/470function concat(array, ...values);471```472473#### fromPairs474Returns an object composed from key-value pairs.475476```javascript { .api }477/**478* Returns an object composed from key-value pairs479* @param pairs - The key-value pairs480* @returns Returns the new object481*/482function fromPairs(pairs);483```484485### Conditional Array Operations486487#### dropWhile488Creates a slice of array excluding elements dropped from the beginning.489490```javascript { .api }491/**492* Creates a slice of array excluding elements dropped from the beginning493* @param array - The array to query494* @param predicate - The function invoked per iteration495* @returns Returns the slice of array496*/497function dropWhile(array, predicate);498```499500#### dropRightWhile501Creates a slice of array excluding elements dropped from the end.502503```javascript { .api }504/**505* Creates a slice of array excluding elements dropped from the end506* @param array - The array to query507* @param predicate - The function invoked per iteration508* @returns Returns the slice of array509*/510function dropRightWhile(array, predicate);511```512513#### takeWhile514Creates a slice of array with elements taken from the beginning.515516```javascript { .api }517/**518* Creates a slice of array with elements taken from the beginning519* @param array - The array to query520* @param predicate - The function invoked per iteration521* @returns Returns the slice of array522*/523function takeWhile(array, predicate);524```525526#### takeRightWhile527Creates a slice of array with elements taken from the end.528529```javascript { .api }530/**531* Creates a slice of array with elements taken from the end532* @param array - The array to query533* @param predicate - The function invoked per iteration534* @returns Returns the slice of array535*/536function takeRightWhile(array, predicate);537```538539### Array Searching & Indexing540541#### lastIndexOf542Gets the index at which the last occurrence of value is found in array.543544```javascript { .api }545/**546* Gets the index at which the last occurrence of value is found in array547* @param array - The array to inspect548* @param value - The value to search for549* @param fromIndex - The index to search from550* @returns Returns the index of the matched value, else -1551*/552function lastIndexOf(array, value, fromIndex = array.length - 1);553```554555#### sortedIndexOf556Performs a binary search of a sorted array and returns the index of the first matching value.557558```javascript { .api }559/**560* Performs a binary search of a sorted array and returns the index of the first matching value561* @param array - The sorted array to inspect562* @param value - The value to search for563* @returns Returns the index of the matched value, else -1564*/565function sortedIndexOf(array, value);566```567568#### sortedLastIndex569Uses a binary search to determine the highest index at which value should be inserted.570571```javascript { .api }572/**573* Uses a binary search to determine the highest index at which value should be inserted574* @param array - The sorted array to inspect575* @param value - The value to evaluate576* @returns Returns the index at which value should be inserted577*/578function sortedLastIndex(array, value);579```580581#### sortedLastIndexBy582Like sortedLastIndex but accepts iteratee for computing the sort ranking.583584```javascript { .api }585/**586* Like sortedLastIndex but accepts iteratee for computing the sort ranking587* @param array - The sorted array to inspect588* @param value - The value to evaluate589* @param iteratee - The iteratee invoked per element590* @returns Returns the index at which value should be inserted591*/592function sortedLastIndexBy(array, value, iteratee);593```594595#### sortedLastIndexOf596Like lastIndexOf but performs a binary search on a sorted array.597598```javascript { .api }599/**600* Like lastIndexOf but performs a binary search on a sorted array601* @param array - The sorted array to inspect602* @param value - The value to search for603* @returns Returns the index of the matched value, else -1604*/605function sortedLastIndexOf(array, value);606```607608### Advanced Array Manipulation609610#### pullAll611Removes all given values from array using SameValueZero for equality comparisons.612613```javascript { .api }614/**615* Removes all given values from array using SameValueZero for equality comparisons616* @param array - The array to modify617* @param values - The values to remove618* @returns Returns array619*/620function pullAll(array, values);621```622623#### pullAllBy624Like pullAll but accepts iteratee for specifying how elements are compared.625626```javascript { .api }627/**628* Like pullAll but accepts iteratee for specifying how elements are compared629* @param array - The array to modify630* @param values - The values to remove631* @param iteratee - The iteratee invoked per element632* @returns Returns array633*/634function pullAllBy(array, values, iteratee);635```636637#### pullAt638Removes elements from array corresponding to indexes and returns an array of removed elements.639640```javascript { .api }641/**642* Removes elements from array corresponding to indexes and returns an array of removed elements643* @param array - The array to modify644* @param indexes - The indexes of elements to remove645* @returns Returns the new array of removed elements646*/647function pullAt(array, ...indexes);648```649650### Set Operations (Extended)651652#### unionBy653Like union but accepts iteratee for specifying how elements are compared.654655```javascript { .api }656/**657* Like union but accepts iteratee for specifying how elements are compared658* @param arrays - The arrays to inspect659* @param iteratee - The iteratee invoked per element660* @returns Returns the new array of combined values661*/662function unionBy(...arrays, iteratee);663```664665#### unionWith666Like union but accepts comparator for specifying how elements are compared.667668```javascript { .api }669/**670* Like union but accepts comparator for specifying how elements are compared671* @param arrays - The arrays to inspect672* @param comparator - The comparator invoked per element673* @returns Returns the new array of combined values674*/675function unionWith(...arrays, comparator);676```677678#### xorBy679Like xor but accepts iteratee for specifying how elements are compared.680681```javascript { .api }682/**683* Like xor but accepts iteratee for specifying how elements are compared684* @param arrays - The arrays to inspect685* @param iteratee - The iteratee invoked per element686* @returns Returns the new array of filtered values687*/688function xorBy(...arrays, iteratee);689```690691#### xorWith692Like xor but accepts comparator for specifying how elements are compared.693694```javascript { .api }695/**696* Like xor but accepts comparator for specifying how elements are compared697* @param arrays - The arrays to inspect698* @param comparator - The comparator invoked per element699* @returns Returns the new array of filtered values700*/701function xorWith(...arrays, comparator);702```703704#### unzipWith705Like unzip but accepts iteratee to specify how regrouped values should be combined.706707```javascript { .api }708/**709* Like unzip but accepts iteratee to specify how regrouped values should be combined710* @param array - The array of grouped elements to process711* @param iteratee - The function to combine regrouped values712* @returns Returns the new array of regrouped elements713*/714function unzipWith(array, iteratee);715```716717## Usage Examples718719```javascript720import {721chunk, compact, difference, flatten, uniq,722zip, head, last, indexOf, take, drop723} from "lodash";724725// Chunking arrays726const items = [1, 2, 3, 4, 5, 6, 7, 8];727const batches = chunk(items, 3);728// Result: [[1, 2, 3], [4, 5, 6], [7, 8]]729730// Cleaning arrays731const mixed = [0, 1, false, 2, '', 3, null, 4, undefined, 5];732const clean = compact(mixed);733// Result: [1, 2, 3, 4, 5]734735// Set operations736const arr1 = [1, 2, 3, 4];737const arr2 = [2, 3, 5, 6];738const diff = difference(arr1, arr2);739// Result: [1, 4]740741// Flattening742const nested = [1, [2, 3], [4, [5, 6]]];743const flat = flatten(nested);744// Result: [1, 2, 3, 4, [5, 6]]745const deepFlat = flattenDeep(nested);746// Result: [1, 2, 3, 4, 5, 6]747748// Deduplication749const duplicates = [1, 2, 2, 3, 3, 3, 4];750const unique = uniq(duplicates);751// Result: [1, 2, 3, 4]752753// Zipping arrays754const names = ['John', 'Jane'];755const ages = [30, 25];756const pairs = zip(names, ages);757// Result: [['John', 30], ['Jane', 25]]758759// Array access760const numbers = [10, 20, 30, 40, 50];761const first = head(numbers); // 10762const lastItem = last(numbers); // 50763const firstThree = take(numbers, 3); // [10, 20, 30]764const skipTwo = drop(numbers, 2); // [30, 40, 50]765```