Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

list
node.js
communitycreator

Useful functions in List Collection in Node.js

Harsh Jain

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.

Installation

To install the collections Node package, you can run the command below in your local system:

npm i collections
Install the Collections package in Node.js

You can also execute the command in the terminal below to install the collections package.

Terminal 1
Terminal

Functions

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.

Code

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() function
console.log("List before calling push(): ", list_one.toArray());

// Using the push() function
list_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() function
var 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() function
var 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() function
var 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() function
list_one.poke("NodeJS");
console.log("List after calling poke(): ", list_one.toArray());

// Using the peek() function
var element_beg = list_one.peek();
console.log("Element at the beginning of the List: ", element_beg);
Useful functions in List collection in Node.js

Explanation

  • In line 1, we import the required package.-
  • In line 3, we create an object of List collection and set three values in the collection.
  • In line 5, we print the number of elements present in the collection using the length property of the List collection.
  • In line 8, we use the toArray() function to print the element of the List collection by converting them into an array.
  • From lines 11 to 13, we call the push() function and insert new elements from the end of the collection.
  • In line 15 and 16, we print the number of elements and the elements present in the collection.
  • In line 19, we call the pop() function to remove the element from the end of the collection.
  • In line 20, we print the removed element.
  • In line 24, we call the shift() function to remove the element from the beginning of the collection.
  • In line 25, we print the removed element.
  • In line 29, we call the delete() function and pass the element that we want to delete from our List collection.
  • In line 34, we call the poke() function and pass the element that will replace with element present at the beginning of the collection.
  • In line 35, we can observe that the element NodeJS has replaced the first element in list.
  • In line 38, we call the peek() function to get the element present at the beginning of the collection.
  • In line 39, we print the element that was peeked in the above step.

RELATED TAGS

list
node.js
communitycreator
RELATED COURSES

View all Courses

Keep Exploring