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