ArrayConverter

The ArrayConverter class provides methods to create an array from a set of values.

Description

The ArrayConverter class provides methods to create an array from a set of values. These values can be in the form of a list, a single value or a string of comma-delimited values.

Static methods

listToArray

Converts a list into an array object with an empty array as default. Strings with comma-delimited values are split into array of strings. See toArray

public static listToArray(value: any): any[]

  • value: any - list to convert.
  • returns: any[] - array object or empty array when value is null.

toArray

Converts a value into an array object with an empty array as default. Single values are converted into arrays with a single element.

public static toArray(value: any): any[]

  • value: any - value to convert.
  • returns: any[] - array object or empty array when value is null.

toArrayWithDefault

Converts a value into an array object with a specified default. Single values are converted into arrays with a single element.

public static toArrayWithDefault(value: any, defaultValue: any[]): any[]

  • value: any - value to convert.
  • defaultValue: any[] - default array object.
  • returns: any[] - array object or default array when value is null.

toNullableArray

Converts a value into an array object. Single values are converted into arrays with a single element.

public static toNullableArray(value: any): any[]

  • value: any - value to convert.
  • returns: any[] - array object or null when value is null.

Examples

// Array
let value1 = ArrayConverter.toArray([1, 2]) ;      // Result: [1, 2]

// Single value
let value2 = ArrayConverter.toArray(1);            // Result: [1]

// String with comma-delimited values
let value3 = ArrayConverter.listToArray("1,2,3"); // Result: ["1", "2", "3"]