Server Data Formatters - Collections

Intro

Below is a list of Collections data formatters available in Server Connect, accompanied by explanations outlining the purpose and functionality of each.

Collections Formatters

We have a database query people which returns info about people:

    "people": [
        {
            "id": 1,
            "name": "Alice",
            "age": 30
        },
        {
            "id": 2,
            "name": "John",
            "age": 27
        },
        {
            "id": 3,
            "name": "Bob",
            "age": 21
        }
    ]

We will use this data in the examples below.

Join:

  • Description: Joins the values of selected property of an array into a string using a specified delimiter.
  • Example: people.join(',', 'name') returns "Alice,John,Bob"

Top:

  • Description: Retrieves the first N elements from an array.
  • Example: people.top(2) returns [{ "id": 1, "name": "Alice", "age": 30 }, { "id": 2, "name": "John", "age": 27 }]

Last:

  • Description: Retrieves the last N elements from an array.
  • Example: people.last(2) returns [ { "id": 2, "name": "John", "age": 27 }, { "id": 3, "name": "Bob", "age": 21 } ]

Where:

  • Description: Filters an array of objects based on a specified property, operator, and value.
  • Example: people.where('age', '==', 30) returns [ { "id": 1, "name": "Alice", "age": 30 } ]

Unique:

  • Description: Removes duplicate values from an array and extracts a property's values.
  • Example: people.unique('name') returns [ "Alice", "John", "Bob" ]

GroupBy:

  • Description: Groups an array of objects by a specified property.
  • Example: people.groupBy('age') returns { "21": [ { "id": 3, "name": "Bob", "age": 21 } ], "27": [ { "id": 2, "name": "John", "age": 27 } ], "30": [ { "id": 1, "name": "Alice", "age": 30 } ] }

Sort:

  • Description: Sorts an array in ascending order, based on a specified property.
  • Example: people.sort('age') returns [ { "id": 3, "name": "Bob", "age": 21 }, { "id": 2, "name": "John", "age": 27 }, { "id": 1, "name": "Alice", "age": 30 } ]

Randomize:

  • Description: Shuffles the elements of an array randomly.
  • Example: people.randomize() returns a shuffled array

Reverse:

  • Description: Reverses the order of elements in an array.
  • Example: people.reverse() returns [ { "id": 3, "name": "Bob", "age": 21 }, { "id": 2, "name": "John", "age": 27 }, { "id": 1, "name": "Alice", "age": 30 } ]

Count:

  • Description: Returns the number of elements in an array.
  • Example: people.count() returns 3

Min:

  • Description: Returns the minimum value of an array or property values in an array of objects.
  • Example: people.min('age') returns 21

Max:

  • Description: Returns the maximum value of an array or property values in an array of objects.
  • Example: people.max('age') returns 30

Sum:

  • Description: Calculates the sum of numeric values in an array or property values in an array of objects.
  • Example: people.sum('age') returns 78

Avg:

  • Description: Calculates the average of numeric values in an array or property values in an array of objects.
  • Example: people.avg('age') returns 26

Keys:

  • Description: Returns an array of keys from an associative array or object.
  • Example: people.keys() returns [ 0, 1, 2 ]

Values:

  • Description: Returns an array of values from an associative array or object.
  • Example: people.values() returns [ { "id": 1, "name": "Alice", "age": 30 }, { "id": 2, "name": "John", "age": 27 }, { "id": 3, "name": "Bob", "age": 21 } ]

Flatten:

  • Description: Extracts values of a specified property from an array of objects and returns a flattened array.
  • Example: people.flatten('name') returns [ "Alice", "John", "Bob" ]
6 Likes