Array Operators

Array operators in DataLogic-rs are used to work with arrays or lists in JSON data. They help you filter, transform, and analyze array elements.

map
filter
reduce
all
some
none
merge
in
length
slice
sort

map

The map operator transforms each item in an array or object using a specified expression. It applies a transformation function to each element of an array or each value of an object and returns a new array/object containing the results.

Syntax

{ "map": [source, transformation_expression] }

Parameters

Parameter Description
source The array or object to transform
transformation_expression The expression to apply to each element. Use {"val": []} to access the current element, {"val": "_key"} for object keys.

Example: Double each number

Rule:

Data:

Result:

Example: Extract property from objects

Rule:

Data:

Result:

Example: Map over object properties

Rule:

Data:

Result:

some

The some operator checks if at least one element in an array satisfies a condition.

Syntax

{ "some": [array, condition] }

Parameters

Parameter Description
array The array to check
condition The condition to test each element against

Example: Check if any item has a quantity of 10 or more

Rule:

Data:

Result:

all

The all operator checks if all elements in an array satisfy a condition.

Syntax

{ "all": [array, condition] }

Parameters

Parameter Description
array The array to check
condition The condition to test each element against

Example: Check if all users are adults

Rule:

Data:

Result:

none

The none operator checks if no elements in an array satisfy a condition. It's effectively the opposite of the some operator.

Syntax

{ "none": [array, condition] }

Parameters

Parameter Description
array The array to check
condition The condition to evaluate for each element

Example: Check if none of the values are negative

Rule:

Data:

Result:

Example: Check if none of the items are in stock

Rule:

Data:

Result:

filter

The filter operator creates a new array with all elements that pass a condition.

Syntax

{ "filter": [array, condition] }

Parameters

Parameter Description
array The array to filter
condition The condition each element must satisfy

Example: Filter out even numbers

Rule:

Data:

Result:

Example: Filter for adult users

Rule:

Data:

Result:

Example: Filter Products by Price and Availability

Rule:

Data:

Result:

reduce

The reduce operator applies a function to each element in the array to reduce it to a single value.

Syntax

{ "reduce": [array, accumulator_function, initial_value] }

Parameters

Parameter Description
array The array to reduce
accumulator_function The function to apply to each element and the accumulator
initial_value The initial value of the accumulator

Example: Sum of numbers

Rule:

Data:

Result:

Example: Calculating total order value

Rule:

Data:

Result:

merge

The merge operator combines multiple arrays into a single array.

Syntax

{ "merge": [array1, array2, ...] }

Parameters

Parameter Description
array1, array2, ... Arrays to merge together

Example: Merge multiple arrays

Rule:

Data:

Result:

Example: Merge and flatten nested arrays

Rule:

Data:

Result:

in

The in operator checks if a specific value exists in an array.

Syntax

{ "in": [value, array] }

Parameters

Parameter Description
value The value to search for
array The array to search in

Example: Check if a value is in an array

Rule:

Data:

Result:

Example: Check for permissions

Rule:

Data:

Result:

length

The length operator returns the number of elements in an array or the number of characters in a string.

Syntax

{ "length": value }

Parameters

Parameter Description
value The array or string to get the length of

Example: Get array length

Rule:

Data:

Result:

Example: Get string length

Rule:

Data:

Result:

Notes

  • For strings, returns the number of characters (Unicode code points), not bytes.
  • Will throw an error if the value is not an array or string.

slice

The slice operator extracts a portion of an array or string based on start, end, and step parameters.

Syntax

{ "slice": [collection, start, end, step] }

Parameters

Parameter Description Required
collection The array or string to slice Yes
start The starting index (inclusive). Negative indices count from the end. No (defaults to 0)
end The ending index (exclusive). Negative indices count from the end. No (defaults to length)
step The step value. Negative values iterate in reverse. No (defaults to 1)

Example: Basic array slicing

Rule:

Data:

Result:

Example: Negative indices

Rule:

Data:

Result:

Example: With step value

Rule:

Data:

Result:

Example: String slicing

Rule:

Data:

Result:

Example: Reverse a string with negative step

Rule:

Data:

Result:

Notes

  • Negative indices count from the end of the array or string (-1 is the last element).
  • Specifying null for end uses the collection's length by default.
  • Using a negative step iterates through the collection in reverse order.
  • If a step of 0 is given, an error will be thrown.

sort

The sort operator sorts an array in ascending or descending order, with optional field extraction for sorting objects.

Syntax

{ "sort": [array, direction, field_extractor] }

Parameters

Parameter Description Required
array The array to sort Yes
direction Boolean or string indicating sort direction: true/false, "asc"/"desc" No (defaults to true/ascending)
field_extractor Expression to extract sort values from objects No

Example: Basic sorting

Rule:

Data:

Result:

Example: Descending order

Rule:

Data:

Result:

Example: Sort by field

Rule:

Data:

Result:

Example: Sort by complex expression

Rule:

Data:

Result:

Notes

  • When sorting mixed data types, the following order is used: null < boolean < number < string < array < object.
  • For direction, both boolean values (true/false) and strings ("asc"/"desc") are supported.
  • The field_extractor is evaluated for each array element to determine its sort key.
  • Complex sort expressions can be used to sort by calculated values.
  • Sorts are stable, meaning equal elements maintain their original order.

Array Operators Overview

DataLogic-rs provides powerful array operators that help you work with arrays:

  • map - Transform each element in an array
  • filter - Remove elements that don't match a condition
  • reduce - Combine array elements into a single value
  • all - Check if all elements match a condition
  • some - Check if at least one element matches a condition
  • none - Check if no elements match a condition
  • merge - Combine multiple arrays into one
  • in - Check if a value exists in an array

These operators can be combined to perform complex array operations efficiently.