Map functions
#
putThe put() function takes key and value as arguments, and adds the key-value pair to the mapVariable.
Syntax:
mapVariable
.put(String
key,Object
value);
Parameters:
Param | Data type | Description |
---|---|---|
key | string | The key to be added to the map. |
Value | string | The value to be added to the map. |
Returns: Void
Example:
key = "name";value = "Facilio";
mapObject = {};mapObject.put(key, value);
log mapObject; // { name: "Facilio" }
#
putAllThe putAll() function takes mapVariableOne and mapVariableTwo as arguments, and adds all key-value pairs present in mapVariableTwo to mapVariableOne.
Syntax:
mapVariableOne
.putAll(Map
mapVariableTwo);
Parameters:
Param | Data type | Description |
---|---|---|
anotherMap | Map | The map object to be added |
Returns: Void
Example:
mapVariableOne = {};mapVariableOne.a = 1;
mapVariableTwo = {};mapVariableTwo.b = 2;mapVariableTwo.c = 3;
mapVariableOne.putAll(mapVariableTwo);
log mapVariableOne; // { a: 1, b: 2, c: 3 }
#
removeThe remove() function takes key as arguments, and removes the specified key along with its value from mapVariable.
Syntax:
mapVariable
.remove(String
key);
Parameters:
Param | Data type | Description |
---|---|---|
key | String | The key which will be removed along with the value. |
Returns: Void
Example:
mapVariable = {};mapVariable.a = 1;mapVariable.b = 2;mapVariable.c = 3;
mapVariable.remove("a");
log mapVariable; // { b: 2, c: 3 }
#
removeAllThe removeAll() function takes list of keys as arguments, and removes the specified keys along with its values from mapVariable.
Syntax:
mapVariable
.removeAll(List
keys);
Parameters:
Param | Data type | Description |
---|---|---|
keys | List | The list of keys which will be removed along with the values. |
Returns: Void
Example:
mapVariable = {};mapVariable.a = 1;mapVariable.b = 2;mapVariable.c = 3;
removeKeys = ["b", "c"];mapVariable.removeAll(removeKeys);
log mapVariable; // { a: 1 }
#
getThe get() function takes mapVariable and searchKey as arguments. It returns the value mapped to the searchKey in mapVariable.
Note :If the key is not found, the function will return null
Syntax:
mapVariable
.get(String
searchKey);
Parameters:
Param | Data type | Description |
---|---|---|
searchKey | String | The key whose corresponding value will be returned. |
Returns: The return type will depend on the data type of the returned value.
Example:
mapVariable = {};mapVariable.a = 1;mapVariable.b = 2;mapVariable.c = 3;
value = mapVariable.get("a");log value; // 1
#
sizeThe size() function takes a mapVariable as argument, and returns the count of key-value pairs in the map.
Syntax:
mapVariable
.size();
Returns: Number
Example:
mapVariable = {};mapVariable.a = 1;mapVariable.b = 2;mapVariable.c = 3;
size = mapVariable.size();log size; // 3
#
parseThe parse() function takes a JSON formatted text as an argument, and returns a key-value pair map object.
Syntax:
new NameSpace("map").parse(
String
jsonString)
Returns: Map
Example:
jsonString = "{\"employee\":{\"name\":\"User\",\"id\":1,\"role\":\"Manager\",\"department\":\"Engineering\"}}"; // escaped json string
parsedMap = new NameSpace("map").parse(jsonString);log parsedMap; // {"employee":{"name":"User","id":1,"role":"Manager","department":"Engineering"}}
#
toStringThe toString() function takes a mapVariable as argument, and returns the JSON formatted text.
Syntax:
mapVariable
.toString();
Returns: String
Example:
mapVariable = {};mapVariable.a = 1;mapVariable.b = 2;mapVariable.c = 3;
json = mapVariable.toString();log json; // 3
#
keysThe keys() function takes a mapVariable as argument, and returns the keys of the given map.
Syntax:
mapVariable
.keys();
Returns: list
Example:
mapVariable = {};mapVariable.a = 1;mapVariable.b = 2;mapVariable.c = 3;json = mapVariable.keys();log json; // [a,b,c]
#
clearThe clear() function removes all the specified keys along with its values from mapVariable.
Syntax:
mapVariable
.clear();
Returns: Void
Example:
mapVariable = {};mapVariable.a = 1;mapVariable.b = 2;mapVariable.c = 3;mapVariable.clear();log mapVariable; // {}