Skip to main content

Helper methods

Use these helper API methods to reformat data.

The _tool object exposes helper methods for reformatting data.

formatDataAsArray()

Formats data as an array of objects or an array of arrays.

datarequired
object

The object to convert into an array.

outputArrayOfArrays
boolean

Whether to return an array of arrays. Defaults to false.

Example

Format data returned from a SQL query as an array, where each item represents one row of the query result.

return _tool.formatDataAsArray({
id: [2, 5],
first_name: ["Rosemary", "Elizabeth"],
last_name: ["Rogers", "Meets"],
});

formatDataAsObject()

Formats data as an object.

datarequired
array

The array to turn into an object.

Example

Return a Table component's changesetArray property, an array, as an object grouped by common keys.

return _tool.formatDataAsObject([
{
id: 2,
first_name: "Rosemary",
last_name: "Rogers",
},
{
id: 5,
first_name: "Elizabeth",
last_name: "Meets",
},
]);

swap()

Accepts an array with two indices, then returns a copy of the array with the values at the two indices swapped. This method is primarily used to reorder two indices in a data source for a List View component.

datarequired
array

The array to copy and reorder.

Example

const fruits = ['apple', 'banana', 'cantaloupe', 'dragonfruit', 'elderberry']
return _tool.swap(fruits, 0, 4)