Skip to main content

datapipe-js/array

datapipe-js/array contains a set of functions to transform, aggregate and join JavaScript arrays.

Data Transformation functions#

groupBy#

groupBy(
array: any[],
groupByFields: string | string[] | Selector
): any[]

Groups array items based on elementSelector function

pivot#

Returns a reshaped array based on unique column values.

pivot(
array: any[],
rowFields: string | string[],
columnField: string,
dataField: string,
aggFunction?: (array: any[]) => any | null, columnValues?: string[]
): any[]
  • array array to pivot
  • rowFields row fields (or index fields). It can be one or more field names
  • columnField a field which values will be used to create columns
  • dataField a dataField which will be aggrated with aggregate function and groupped by rows and columns
  • aggFunction an aggregation function. Default value is sum. data field will be aggregated by this function
  • columnValues an optional initial column values. Use it to define a set of columns/values you would expect

transpose#

Returns a transposed array, where rows become columns

transpose(data: any[]): any[]

select#

Creates new array based on selector.

  • array The array to process.
  • elementSelector A Selector function that is invoked per iteration.
select(
data: any[],
selector: string | string[] | Selector
): any[]

map#

Alias for select and has same usage

where#

Filters array based on predicate function.

  • array The array to process.
  • predicate A predicate function to filter items.
where(
data: any[],
predicate: Predicate
): any[]

filter#

Alias for where and has same usage

flattern#

Returns a flatern array.

  • array The array to flatten recursively.
flatten(array: any[]): any[]

Example

flatten([1, 4, [2, [5, 5, [9, 7]], 11], 0]); // length 9

sort#

A simple sort array function with a convenient interface

  • array The array to process.
  • fields field names with a sort order.
sort(array: any[], ...fields: string[]): any[]

example

sort(array, 'name ASC', 'age DESC');

Joining JavaScript arrays#

leftJoin#

Returns a joined array with all elements from the left array (leftArray), and the matched elements from the right array (rightArray). The result is NULL from the right side, if there is no match.

  • leftArray array for left side in a join
  • rightArray array for right side in a join
  • leftKey A key from left side array. What can be as a fieldName, multiple fields or key Selector
  • rightKey A key from right side array. what can be as a fieldName, multiple fields or key Selector
  • resultSelector A callback function that returns result value */
leftJoin(
leftArray: any[],
rightArray: any[],
leftKeySelector: string | string[] | Selector<any, string>,
rightKeySelector: string | string[] | Selector<any, string>,
resultSelector: (leftItem: any, rightItem: any) => any
): any[]

innerJoin#

Joins two arrays together by selecting elements that have matching values in both arrays. If there are elements in any array that do not have matches in other array, these elements will not be shown!

  • leftArray array for left side in a join
  • rightArray array for right side in a join
  • leftKey A key from left side array. What can be as a fieldName, multiple fields or key Selector
  • rightKey A key from right side array. what can be as a fieldName, multiple fields or key Selector
  • _resultSelecto_r A callback function that returns result value */
innerJoin(
leftArray: any[],
rightArray: any[],
leftKey: string | string[] | Selector<any, string>,
rightKey: string | string[] | Selector<any, string>,
resultSelector: (leftItem: any, rightItem: any) => any
): any[]

fullJoin#

Returns all elements from the left array (leftArray), and all elements from the right array (rightArray). The result is NULL from the right/left side, if there is no match.

  • leftArray array for left side in a join
  • rightArray array for right side in a join
  • leftKey A key from left side array. What can be as a fieldName, multiple fields or key Selector
  • rightKey A key from right side array. what can be as a fieldName, multiple fields or key Selector
  • resultSelector A callback function that returns result value */
fullJoin(
leftArray: any[],
rightArray: any[],
leftKey: string | string[] | Selector<any, string>,
rightKey: string | string[] | Selector<any, string>,
resultSelector: (leftItem: any, rightItem: any) => any
): any[]

merge#

Merges elements from two arrays. It appends source element or overrides to target array based on matching keys provided

  • targetArray target array
  • sourceArray source array
  • targetKey tartget key field, arry of fields or field serlector
  • sourceKey source key field, arry of fields or field serlector
merge(
targetArray: any[],
sourceArray: any[],
targetKey: string | string[] | Selector<any, string>,
sourceKey: string | string[] | Selector<any, string>
): any[]

Aggregation and stats functions#

sum#

Sum of items in array.

  • array The array to process.
  • elementSelector Function invoked per iteration.
sum(array: any[], field?: Selector | string): number | null

example

sum([1, 2, 5]); // 8
sum([{ val: 1 }, { val: 5 }], i => i.val); // 6

avg#

Average of array items.

  • array The array to process.
  • elementSelector Function invoked per iteration.
avg(array: any[], field?: Selector | string): number | null

average#

Alias for average and has same usage

min#

Computes the minimum value of array.

  • array The array to process.
  • elementSelector Function invoked per iteration.
min(array: any[], field?: Selector | string): number | Date | null

max#

Computes the maximum value of array.

  • array The array to process.
  • elementSelector Function invoked per iteration.
max(array: any[], field?: Selector | string): number | Date | null

count#

Count of elements in array with optional predicate.

  • array The array to process.
  • predicate Predicate function invoked per iteration.
count(array: any[], predicate?: Predicate): number | null

first#

Gets first item in array that satisfies optional predicate.

  • array The array to process.
  • predicate Predicate function invoked per iteration.
first<T = any>(array: T[], predicate?: Predicate): T | null

last#

Gets last item in array that satisfies optional predicate.

  • array The array to process.
  • predicate Predicate function invoked per iteration.
last<T = any>(array: T[], predicate?: Predicate): T | null

countBy#

Gets counts map of values returned by elementSelector.

  • array The array to process.
  • elementSelector Function invoked per iteration.
countBy(array: any[], elementSelector: Selector): Record<string, number>

mean#

Get mean of an array.

  • array The array to process.
  • field Property name or Selector function invoked per iteration.
mean(array: any[], field?: Selector | string): number | null

quantile#

Get quantile of a sorted array.

  • array The array to process.
  • field Property name or Selector function invoked per iteration.
  • p quantile.
quantile(array: any[], p: number, field?: Selector | string): number | null

variance#

Get sample variance of an array.

  • array The array to process.
  • field Property name or Selector function invoked per iteration.
variance(array: any[], field?: Selector | string): number | null

stdev#

Get the sample standard deviation of an array.

  • array The array to process.
  • field Property name or Selector function invoked per iteration.
stdev(array: any[], field?: Selector | string): number | null

median#

Get median of an array.

  • array The array to process.
  • field Property name or Selector function invoked per iteration.
median(array: any[], field?: Selector | string): number | null