The reject() method is used to filter the given collection of elements using the given callback function. If the callback function returns true, then the element is removed from the resulting collection, otherwise, it is not removed.
Syntax:
collect(array).reject(callback)
Parameters: The collect() method takes one argument that is converted into the collection and then reject() method is applied to it. The reject() method holds the callback as a parameter.
Return Value: This method returns the filtered elements from the collection.
Below example illustrate the reject() method in collect.js:
Example 1:
Javascript
const collect = require( 'collect.js' ); let obj = [ 'Geeks' , 'GFG' , 'neveropen' ]; const collection = collect(obj); const filtered = collection.reject( element => element.length > 4); console.log(filtered.all()); |
Output:
[ 'GFG' ]
Example 2:
Javascript
const collect = require( 'collect.js' ); let obj = [ { name: 'Rahul' , marks: 88 }, { name: 'Aditya' , marks: 78 }, { name: 'Abhishek' , marks: 87 } ]; const collection = collect(obj); const filtered = collection.reject( element => element.name.length > 5); console.log(filtered.all()); |
Output:
[ { name: 'Rahul', marks: 88 } ]