In this shot, we will discuss the Dict.set()
function in Node.js. The Dict collection is available in the collections package. To install the collections package, you can execute the command below:
npm i collections
The Dict.set()
function is used to insert a key-value pair in the dictionary collection.
The syntax of the Dict.set()
function is given below:
void set(K key, V value);
The Dict.set()
function accepts two parameters:
The Dict.set()
function returns a boolean value where true
indicates the new key-value pair has been inserted successfully and false
indicates that the key-value pair is not inserted.
Let’s see how this function is used in the code.
var Dict = require("collections/dict");var dictionary_obj = new Dict({'a': 1, 'b': 2});console.log("Number of elements in Dictionary: ", dictionary_obj.length);console.log("Elements in the Dictionary: ",dictionary_obj.toObject());var is_element_inserted = dictionary_obj.set('c', 3);is_element_inserted = dictionary_obj.set('d', 4);console.log("Are the elements inserted?: ", is_element_inserted)console.log("Number of elements in Dict: ", dictionary_obj.length);console.log("Elements in the Dictionary: ", dictionary_obj.toObject());