Objects
In OneVision Scripts, you can create objects or static dictionaries that contain key-value pairs. They are useful for storing related data where each element has a name (key) and a value, and the keys are not necessarily numerical indices, but rather unique identifiers like strings or particular numbers. Unlike lists, objects are unordered.
Object initialization
Objects can be initialized by writing each key and its value. All keys must be of the same type, and the values can be of any type.
// Example 1
var obj = {
item1: 23,
item2: "apple",
item3: true
};
// Example 2
var obj = {
"item1": 23,
"item2": "apple",
"item3": true
};
// Example 3
var obj = {
1: 23,
2: "apple",
3: true
};
// Example 4
var obj = {
true: 23,
false: "apple",
};
Accessing object elements
You can access the object elements by using the bracket notation, writing the object name followed by the key between [""]:
// Example 1
var obj = {
item1: 23,
item2: "apple",
item3: true
};
var item1 = obj["item1"];
var item2 = obj["item2"];
var item3 = obj["item3"];
// Example 2
var obj = {
"item1": 23,
"item2": "apple",
"item3": true
};
var item1 = obj["item1"];
var item2 = obj["item2"];
var item3 = obj["item3"];
// Example 3
var obj = {
1: 23,
2: "apple",
3: true
};
var item1 = obj["1"];
var item2 = obj["2"];
var item3 = obj["3"];
// Example 4
var obj = {
true: 23,
false: "apple",
};
var item1 = obj["true"];
var item2 = obj["false"];
Nested objects
Objects can contain other objects as values. These are called nested objects and are useful for representing complex data structures.
var obj = {
"item1": 23,
"item2": "apple",
"item3": {
"item31": true,
"item32": 5
}
};
// Access nested properties
var item31 = obj["item3"]["item31"] // item31 = true