In this shot, we will discuss the some of the very useful functions available in the List
collection in Node.js
. The List
collection is available in the collections package. There are many data structure implementations in the collections
package, including List
.
To install the collections
Node package, you can run the command below in your local system:
npm i collections
You can also execute the command in the terminal below to install the collections
package.
In this shot, we will be covering the following functions:
push()
: The push()
function is used to insert the element at the end of the collection. This functions will accept one parameter that will be the value we need to insert in the List
collection.pop()
: The pop()
function is used to delete the element from the end of the collection. This functions will not accept any parameter and will return the element that has been removed.shift()
: The shift()
function is used to remove the element from the beginning of the collection. This function will not accept any parameter and will return the element that has been removed.delete()
: The delete()
function is used to delete the specified element. This function will accept one parameter that is the element that needs to be deleted from the List
collection and return a boolean value where true indicates that the element has been deleted successfully whereas false indicates the element is not deleted (maybe it was present already).poke()
: The poke()
function is used to replace the element present at the beginning of the List
collection. This function will accept one parameter that specifies the element that will be replaced with the element present at the beginning of the collection.peek()
: The peek()
function is used to get the element present at the beginning of the List
collection.toArray()
: The toArray()
function is used to convert a List object to an array. This function does not accept any parameter and returns the array containing the same elements that are present in the List
collection.Let us explore how these functions will work in the code.
var List = require("collections/list");var list_one = new List([1, 2, 3]);console.log("Number of elements in the List: ", list_one.length);// Using the toArray() functionconsole.log("List before calling push(): ", list_one.toArray());// Using the push() functionlist_one.push(10);list_one.push("Hello");list_one.push("Educative.io", "NodeJS", 10.5);console.log("Number of elements in the List: ", list_one.length);console.log("List after calling push(): ", list_one.toArray());// Using the pop() functionvar deleted_element_end = list_one.pop();console.log("Element deleted from the beginning: ", deleted_element_end);console.log("List after calling pop(): ", list_one.toArray());// Using the shift() functionvar deleted_element_beg = list_one.shift();console.log("Element deleted from the beginning: ", deleted_element_beg);console.log("List after calling shift(): ", list_one.toArray());// Using the delete() functionvar deleted_element = list_one.delete("NodeJS");console.log("Element deleted from the List? ", deleted_element);console.log("List after calling delete(): ", list_one.toArray());// Using the poke() functionlist_one.poke("NodeJS");console.log("List after calling poke(): ", list_one.toArray());// Using the peek() functionvar element_beg = list_one.peek();console.log("Element at the beginning of the List: ", element_beg);