Skip to main content

Lists

In OneVision Scripts, lists (also called arrays) are ordered collections of elements. They are intended to hold elements of the same type, but you could also store multiple types using the any type .

List declaration

You can declare a list by specifying the type of elements it will hold followed by []:

// List of numbers
var numbers: number[] = [];

// List of strings
var fruits: string[] = [];

// List of mixed types
var mixedList: any[] = [];

List initialization

Lists can be initialized with elements enclosed in square brackets []:

var numbers: number[] = [1, 2, 3, 4, 5];
var fruits: string[] = ["Apple", "Banana", "Cherry"];
var mixedList: any[] = [1, "Hello", true];

As explained, OneVision can infer the type in most cases when the initial value has a unique type:

// List of numbers
var numbers = [1, 2, 3, 4, 5]; // variable numbers is initialized with number[] type

// List of strings
var fruits = ["Apple", "Banana", "Cherry"]; // variable fruits is initialized with string[] type

Accessing list elements

You can access elements of a list using their zero-based index:

var fruits: string[] = ["Apple", "Banana", "Cherry"];

fruits[0]; // Output: "Apple"
fruits[1]; // Output: "Banana"
fruits[2]; // Output: "Cherry"

List methods and properties

Lists in OneVision come with built-in methods and properties for manipulating and accessing their elements.

List properties

PropertyDescriptionReturned Type
lengthReturns or sets the number of elements in a list.number

Mutator methods

MethodDescriptionReturned type
push(item: T): numberAdds one element to the end of the list and returns the new length.number
pop(): T/undefinedRemoves the last element from the list and returns it, or undefined if the list is empty.T/undefined
shift(): T/undefinedRemoves the first element from the list and returns it, or undefined if the list is empty.T/undefined
unshift(item: T): numberAdds one element to the beginning of the list and returns the new length.number
splice(start: number, deleteCount: number, items?: T): T[]Removes and/or inserts elements in the list and returns the removed elements.T[]
sort(): thisSorts the elements of a list in place and returns the list.this
reverse(): thisReverses the order of the elements in the list in place and returns the list.this
info

? means that the parameter is optional.

Accessor Methods

MethodDescriptionReturned type
concat(items: T[]): T[]Returns a copy of the original list with additional elements concatenated.T[]
includes(searchElement: T): booleanDetermines whether a list includes a certain element, returning true or false as appropriate.boolean
slice(start: number, end?: number): T[]Returns a portion of a list as a new list from start to end (end not included). For both start and end a negative index can be used to indicate an offset from the end of the list. If end is not defined then the slice extends to the end of the list.T[]

Nested lists

Nested lists, also known as multidimensional lists, are lists that contain other lists as their elements. These are useful for representing complex data structures like matrices, grids, or any data that naturally fits into a table-like format.

You can easily define nested lists as:

var matrix: number[][] = [[1,2,3], [4,5,6], [7,8,9]]; 
var value = matrix[1][2]; // value = 6