list_range_fill

Fill missing values in a list

Definition

array list_range_fill(array[object] $entry, int &keySelector, int &stepFunction, &constructor)

Take an array of objects containing the properties string "Name" and number Value, and fill out the array by adding objects attaining the values between the present values. &constructor allows you to specify the format of filler objects, and &stepFunction defines the "step" in between them.

Parameters

array[object] $entry

An array of objects with Name and Value properties (they can have different names).

expression &keySelector

Expression pointing to the the name of the Value property.

expression &stepFunction

Function that defines the step between consecutive objects. Must return an integer.

expression &constructor

Specify the format of the filler objects. Outline an object with properties as needed, and note you can use three specific keys to allow for items in the original payload to be referenced:

  • __previous: the previous object from the input list.
  • __next: the next object from the input list.
  • __key: the calculated value from using &stepFunction.

Returns

array

Array including all the objects from $entry, expanded with objects attaining the values in between the ones already present in $entry.

Example

{
  "input": [
    {
      "Name": "A",
      "Value": 2015
    },
    {
      "Name": "B",
      "Value": 2017
    },
    {
      "Name": "C",
      "Value": 2020
    }
  ]
}
 list_range_fill(input, 
                 &Value, 
                 &add(@,`1`), 
                 &{Name:__previous.Name, 
                 Value: __key, 
                 valuePlusLastYear: join('', [__previous.Name, to_string(__previous.Value)]), 
                 previous:__previous.Value, 
                 next:__next.Value})
[
  {
    "Name": "A",
    "Value": 2015
  },
  {
    "Name": "A",
    "Value": 2016,
    "valuePlusLastYear": "A2015",
    "previous": 2015,
    "next": 2017
  },
  {
    "Name": "B",
    "Value": 2017
  },
  {
    "Name": "B",
    "Value": 2018,
    "valuePlusLastYear": "B2017",
    "previous": 2017,
    "next": 2020
  },
  {
    "Name": "B",
    "Value": 2019,
    "valuePlusLastYear": "B2017",
    "previous": 2017,
    "next": 2020
  },
  {
    "Name": "C",
    "Value": 2020
  }
]