class MaxArray
Bind a Max array object
Create a MaxArray object when you want to bind to a Max array object, either because you want to fetch its value or when you want to modify its contents. To manipulate the contents of the array, either use the .set method, or get the value of the array using .stringify and then use regular JavaScript array functions.
A Max array can contain numbers and strings. It can also contain instances of Dict, MaxString, and even other MaxArray objects. However, you must use the .insert, .append, or .prepend methods to add these objects to the MaxArray. You cannot use the .set method.
Examples
// Create a new Max array with initial contents, then modify the contents.
let max_arr = new MaxArray(1, 2, "three", 4.0);
max_arr.set(10, 9, "eight", 7.0); // update the array contents
// Send a Max array out of an outlet
function bang() {
let arr = new MaxArray("I", "got", "a", "bang");
outlet(0, "array", arr.name);
}
// Refer to an existing Max array by name, then modify the contents.
let max_arr = new MaxArray();
max_arr.name = "frith"; // Now the MaxArray refers to an array named "frith"
max_arr.set(2, 4, "six", 8); // Updates the array in the containing patcher
// Convert a Max array to a JavaScript array
function array(arr_name) {
let max_arr = new MaxArray();
max_arr.name = arr_name; // binds to the Max `array` by name
let contents = max_arr.stringify(); // read the value as a string
let js_arr = JSON.parse(contents); // convert the string to a JavaScript array
}
// Retrieve a Max array, modify it as a JavaScript array, then write it back.
let max_arr = new MaxArray();
max_arr.set(2, "3", "four", 6);
let js_arr = JSON.parse(max_arr.stringify());
post(JSON.stringify(js_arr) + "\n"); // prints "[2, '3', 'four', 6]"
js_arr[1] = 3;
max_arr.parse(JSON.stringify(js_arr));
post(max_arr.stringify() + "\n"); // prints "[2, 3, 'four', 6]"
Index
Constructors
Properties
Methods
- append(args)
- at(index)
- clear()
- delete(index)
- freepeer()
- get()
- insert(index, value)
- parse(value)
- prepend(args)
- replace(index, value)
- set(args)
- stringify()
new MaxArray(args)
new MaxArray(args: (string | number)[]?);
Create a new MaxArray
You can set the name of the array either by passing the name after the argument "@name", or you can set the .name property after creating the MaxArray object.
| Parameter | Type | Description |
|---|---|---|
| args | (string | number)[]? | initial values, possibly followed by attribute pairs |
Example
// These both create an array that binds to the same named array.
var my_arr = new MaxArray("initial_value", "@name", "fred");
var my_arr2 = new MaxArray("initial_value");
my_arr2.name = "fred";
append(args)
Append one or more elements to the end of the MaxArray
You can append numbers, strings, other MaxArray objects, or Dict objects. You can append MaxString objects, but they will be converted to regular strings.
append(...args: (string | number | MaxString | Dict | MaxArray)[]): void;
| Name | Type | Description |
|---|---|---|
| args | (string | number | MaxString | Dict | MaxArray)[] | one or more elements to append. |
at(index)
Get the value at the specified index
at(index: number): string | number | Dict | MaxArray;
| Name | Type | Description |
|---|---|---|
| index | number | index of the element to get |
| Return Value | string | number | Dict | MaxArray | The value at the specified index |
clear()
Remove all elements from the MaxArray
clear(): void;
delete(index)
Delete a single element at the specified index
delete(index: number): void;
| Name | Type | Description |
|---|---|---|
| index | number | index of the element to delete |
freepeer()
Free the native C peer
Frees the array data from the native C peer (created when making a MaxArray object), which is not considered by the JavaScript garbage collector, and may consume lots of memory until the garbage collector decides to run based JS allocated memory. Once called, the MaxArray object is not available for any other use. It's not necessary to call this function, as the memory will be freed eventually, but you can call it whenever you're done with your MaxArray object.
freepeer(): void;
get()
Get the current value of the MaxArray as a JavaScript array
get(): (string | number | Dict | MaxArray)[];
| Name | Type | Description |
|---|---|---|
| Return Value | (string | number | Dict | MaxArray)[] | The current value of the MaxArray as a JavaScript array |
insert(index, value)
Insert a single element at the specified index.
You can insert numbers, strings, other MaxArray objects, or Dict objects. You can insert MaxString objects, but they will be converted to regular strings.
insert(
index: number,
value: string | number | MaxString | Dict | MaxArray,
): void;
| Name | Type | Description |
|---|---|---|
| index | number | index at which to insert the element |
| value | string | number | MaxString | Dict | MaxArray | value to insert. Can be a number, string, Dict, MaxArray, or MaxString object. |
length number read-only
The number of elements in the MaxArray
name string
Get and set the name of the MaxArray
Will bind to an existing Max array with the same name
parse(value)
Update the value of the MaxArray
parse(value: string);
| Name | Type | Description |
|---|---|---|
| value | string | value to parse into an array. Should be a JSON string |
prepend(args)
Insert one or more elements at the start of the MaxArray
You can insert numbers, strings, other MaxArray objects, or Dict objects. You can insert MaxString objects, but they will be converted to regular strings.
prepend(...args: (string | number | MaxString | Dict | MaxArray)[]): void;
| Name | Type | Description |
|---|---|---|
| args | (string | number | MaxString | Dict | MaxArray)[] | one or more elements to insert. |
replace(index, value)
Replace the element at the given index with a new value
You can set numbers, strings, other MaxArray objects, or Dict objects. You can set MaxString objects, but they will be converted to regular strings.
replace(
index: number,
value: string | number | MaxString | Dict | MaxArray,
): void;
| Name | Type | Description |
|---|---|---|
| index | number | index of the element to replace |
| value | string | number | MaxString | Dict | MaxArray | new value for the element. Can be a number, string, Dict, MaxArray, or MaxString object. |
set(args)
Set the value of the MaxArray
This method replaces the entire contents of the MaxArray with the specified values. You can only pass numbers and strings to this method. If you want to add Dict, MaxArray, or MaxString objects, use the .append, .prepend, or .insert methods.
set(...args: (string | number)[]): void;
| Name | Type | Description |
|---|---|---|
| args | (string | number)[] |
size number read-only
The number of elements in the MaxArray
stringify()
Get the current value of the MaxArray as a string
stringify(): string;
| Name | Type | Description |
|---|---|---|
| Return Value | string | The current value of the MaxArray as a string |