parent_child_index

Provide subarrays with consistent indexing

Definition

array[object] parent_child_index(array[object] $array, expression->[number|string|boolean|datetime] $childArraySelector)

For an array $array of objects that share an array property with the same name $childArraySelector, introduces indexing of the $childArraySelector arrays that continues through the entire array.

Parameters

array[object] $array

Array of objects that have a key with the same name that has an array value.

expression->array[any] $childArraySelector

An expression that returns an array, most commonly the array identifier.

Returns

The input array, where the objects that are elements of the $childArraySelector arrays have an extra property _calculatedIndex added. The value of _calculatedIndex is equal to the ordinal number of the object when considered across all $childArraySelector arrays from the top down.

Example

{
  "employees":[
    {
      "department": "sales",
      "members":[{"name": "Anna"}]
    },
    {
      "department": "legal",
      "members":[{"name": "Bob"}, {"name": "Clara"}]
    },
    {
      "department":"engineering",
      "members":[{"name": "David"}}]
}
{
  employeesIndexed: parent_child_index(employees, members)
}
{
  "employeesIndexed":[
    {
      "department": "sales",
      "members":[{"name": "Anna","_calculatedIndex": 1}]
    },
    {
      "department": "legal",
      "members":[
        {"name": "Bob","_calculatedIndex": 2},
        {"name": "Clara","_calculatedIndex": 3}]
    },
    {
      "department": "engineering",
      "members":[{"name": "David","_calculatedIndex": 4}]
    }]
}