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.
data
requiredobject
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.
- JavaScript
- Response
return _tool.formatDataAsArray({
id: [2, 5],
first_name: ["Rosemary", "Elizabeth"],
last_name: ["Rogers", "Meets"],
});
[
{
"id": 2,
"first_name": "Rosemary",
"last_name": "Rogers"
},
{
"id": 5,
"first_name": "Elizabeth",
"last_name": "Meets"
}
]
formatDataAsObject()
Formats data as an object.
data
requiredarray
The array to turn into an object.
Example
Return a Table component's changesetArray
property, an array, as an object grouped by common keys.
- JavaScript
- Response
return _tool.formatDataAsObject([
{
id: 2,
first_name: "Rosemary",
last_name: "Rogers",
},
{
id: 5,
first_name: "Elizabeth",
last_name: "Meets",
},
]);
{
"id": [2, 5],
"first_name": ["Rosemary", "Elizabeth"],
"last_name": ["Rogers", "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.
data
requiredarray
The array to copy and reorder.
Example
- JavaScript
- Response
const fruits = ['apple', 'banana', 'cantaloupe', 'dragonfruit', 'elderberry']
return _tool.swap(fruits, 0, 4)
['elderberry', 'banana', 'cantaloupe', 'dragonfruit', 'apple']