Tuesday, December 31, 2024
Google search engine
HomeLanguagesJavascriptCollect.js whereInstanceOf() function

Collect.js whereInstanceOf() function

The whereInstanceOf() function is used to filter input with a given class type.  In JavaScript, the array is first converted to a collection and then the function is applied to the collection.

Syntax: 

data.whereInstanceOf('key')

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

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

Return value: Return the collection with a key value that was mentioned.
Below examples illustrate the whereInstanceOf() function in collect.js
Example 1: Here in this example, we take a collection and then using the whereInstanceOf() method we have returned filtered collection using the key.

Javascript




// It is used to import collect.js library
const collect = require('collect.js');
  
const Input = collect([
  new Books('Pride and prejudice'),
  new Books('The Great Gatsby '),
  new Movies('It'),
]);
  
const output = Input.whereInstanceOf(Books);
console.log(output.all());


Output: 

[
   new Books('Pride and prejudice'),
   new Books('The Great Gatsby'),
]

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

Javascript




// It is used to import collect.js library
const collect = require('collect.js');
  
const Input = collect([
  new Year('1980'),
  new Year('2020'),
  new Name('Mohan'),
]);
  
const output = Input.whereInstanceOf(Name);
  
console.log(output.all());


Output: 

[
  new Name('Mohan'),
]

References: https://collect.js.org/api/whereinstanceof.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