foreach()

Performs operations on an array or between multiple arrays without taking the element out of the array.

Syntax

foreach(OP_EXPR, LIST_EXPR[, ...])
Required Parameter
OP_EXPR
Expression to be performed between array elements. It uses _1 for the elements of the first array, _2 for the elements of the second array, and _N for the elements of the Nth array.
LIST_EXPR, ...
Expressions that return an array and separate them using commas (,).

Description

If the lengths of the arrays passed to the parameters are not the same, the function performs an operation after adding the elements to which null is assigned in the short array according to the number of elements constituting the long array. For example, if the first array consists of five elements and the second array consists of three elements, the command performs an operation after adding two elements whose values are null to the second array.

When a scalar value is passed as an argument instead of a list, the function replicates and extends the value to a list and performs an operation according to OP_EXPR by replacing the first list in the manner of _1 and the second list in the manner of _2, respectively.

Usage

json "{}" 
| eval arr1= array(-1, -2, -3, -4, -5), arr2= array(1,2,3,4,5) 
| eval _output = foreach(_1 * _2, arr1, arr2) 
| order arr1, arr2, _output
=> [-1,-4,-9,-16,-25]