Object Functions

Objects in Flowata are collections of key-value pairs. You can access, modify, or manage their properties using various helper functions. They are created with the {} syntax.

clone(object1, object2, ...)

Creates a deep copy of the provided object(s). If multiple objects are provided, they are merged together. The properties of later objects will overwrite those of earlier objects in case of conflicts.

Examples:

  1. Cloning a single object:
setLocal(originalObj, {name: "John", age: 30});
setLocal(duplicateObj, clone(originalObj));

setLocal(duplicateObj.name, "Doe"); // Update the name property in the duplicate object

print(originalObj); // Output: {name: "John", age: 30}
print(duplicateObj); // Output: {name: "Doe", age: 30}
  1. Cloning and merging multiple objects
setLocal(obj1, {name: "John", age: 30});
setLocal(obj2, {age: 25, city: "New York"});

setLocal(mergedObj, clone(obj1, obj2));

print(mergedObj); // Output: {name: "John", age: 25, city: "New York"}

In the above example, the age property from obj2 overwrites the age property from obj1 in the merged object.

Setting Object Values

To update a value within an object, you can use dot notation or bracket notation:

setLocal(person, {name: "John", age: 30});
setLocal(person.name, "Jane"); // Using dot notation
setLocal(person["age"], 25); // Using bracket notation when reading from a variable
setLocal(keyName, 'age');
print(person[keyName]); // output: 25

print(person); // Output: {name: "Jane", age: 25}

updateObject(object, key, newValue)

Updates the value associated with a key in an object. This function provides an alternative to using setLocal for setting values within objects, especially when dealing with nested properties.

Examples:

  1. Using Direct Key

    setLocal(obj, {key1: "value1", key2: "value2"});
    updateObject(obj, "key2", "new value");
    print(obj.key2); // Output: "new value"
  2. Using dotPath

    Using dotPath provides a way to navigate nested objects easily. Remember, when using updateObject, you don't need the prefix of the object in the dotPath.

    setLocal(myObj, {foo: {bar: "old value"}});
    updateObject(myObj, "foo.bar", "new value");
    print(myObj.foo.bar); // Output: "new value"

Note: When using updateObject with dotPath, ensure that the key provided is a valid path within the object to avoid unintended behavior. If the path doesn't exist in the object, the function will create the necessary nested structure.

keys(object)

Returns an array of the object's own enumerable property names.

setLocal(obj, {a: 1, b: 2, c: 3});
print(keys(obj)); // Output: ["a", "b", "c"]

has(object, key)

Checks if an object has a specific key and returns a boolean value.

setLocal(obj, {a: 1, b: 2});
print(has(obj, "a")); // Output: true
print(has(obj, "c")); // Output: false

removeItemByKey(object, key)

Removes a key-value pair from the object based on the provided key.

setLocal(obj, {a: 1, b: 2, c: 3});
setLocal(updatedObj, removeItemByKey(obj, "b"));

print(updatedObj); // Output: {a: 1, c: 3}

length(object)

Returns the number of keys stored in the object.

setLocal(obj, {a: 1, b: 2, c: 3});
print(length(obj)); // Output: 3

clearObject(object)

Removes all key-value pairs from the object, effectively emptying it.

Examples:

setLocal(obj, {a: 1, b: 2, c: 3});
setLocal(clearedObj, clearObject(obj));

print(clearedObj); // Output: {}