Tuesday, December 31, 2024
Google search engine
HomeLanguagesJavascriptCollect.js | forget() Function

Collect.js | forget() Function

The forget() function as the name suggest forgets/remove an item from the collection using its key.  In JavaScript, the array is first converted to a collection and then the function is applied to the collection.
Syntax: 

data.forget('key')

Parameters: This function accept a single parameter as mentioned above and described below:

  • Key: This parameter holds the key name that define the value of that key.

Return value: Return the collection except the key’s value that was mentioned.

Below examples illustrate the forget() function in collect.js
Example 1: Here in this example, we take a collection and then using the forget() function we return the collection without the key element we forget.

Javascript




// It is used to import collect.js library
const collect = require('collect.js');
 
const collection = collect({
  name: 'Jhon',
  Roll: 7,
});
 
collection.forget('Roll');
 
console.log(collection.all());


Output: 

{ name: 'Jhon' }

Example 2: Same thing we have done here as above example.

Python3




// It is used to import collect.js library
const collect = require('collect.js');
 
const collection = collect({
  Address : '253 Street , California',
  pincode : '0987'
});
 
collection.forget('Address');
 
console.log(collection.all());


Output: 

{ pincode: '0987' }

References: https://collect.js.org/api/forget.html

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments