Open In App

Collect.js except() Method

The except() method is used to return all items in the given collection except the specified keys.

Syntax:



collect(array).except(key)

Parameters: The collect() method takes one argument that is converted into the collection and then except() method is applied on it. The except() method takes one parameter as key which you want to remove from given collection.

Return Value: This method returns the collection items except the given keys items.



Below example illustrate the except() method in collect.js:

Example 1:




const collect = require('collect.js');
  
const arr = [2, 4, 5, 6, 8, 9, 10];
  
const collection = collect(arr);
  
const remaining_element = 
    collection.except(5, 7, 9, 10, 12);
  
console.log(remaining_element);

Output:

Collection { items: [ 2, 4, 6, 8 ] }

Example 2:




const collect = require('collect.js');
  
const arr = ['Geeks', 'GFG', 'GeeksforGeeks', 'Welcome'];
  
const collection = collect(arr);
  
const remaining_element = collection.except('GFG');
  
console.log(remaining_element);

Output:

Collection { items: [ 'Geeks', 'GeeksforGeeks', 'Welcome' ] }
Article Tags :