Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Collect.js | whereBetween() Function

  • Last Updated : 10 Jun, 2020

The whereBetween()function is used to filter an input within a given range. In JavaScript, the array is first converted to a collection and then the function is applied to the collection.
Syntax: 

data.whereBetween(key, [range]);

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

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • Key: This parameter holds the key name that define the value of that key.
  • Range: The specified range

Return Value:  Returns the filtered collection within the range.
 

Below examples illustrate the whereBetween()function in collect.js:
Example 1: Here in this example, we take a collection and then using the whereBetween() method we specify a key and the value range, to check the value and return the value lie in the range.



Javascript




// It is used to import collect.js library   
const collect = require('collect.js');
  
const input= collect([
  { fruits: 'Apple', price: 200 },
  { fruits: 'Banana', price: 80 },
  { fruits: 'Papaya', price: 150 },
  { fruits: 'Grapes', price: 30 },
  { fruits: 'Cherry', price: 100 },
]);
  
const output = input.whereBetween('price', [100, 200]);
console.log(output.all());

Output:

[ { fruits: 'Apple', price: 200 },
  { fruits: 'Papaya', price: 150 },
  { fruits: 'Cherry', price: 100 }]

Example 2: 

Javascript




// It is used to import collect.js library  
const collect = require('collect.js');
  
const input= collect([
  { quantity: 'Flour', price: 150 },
  { quantity: 'Rice', price: 100 },
  { quantity: 'Vegetables', price: 80 },
  { quantity: 'Fruits', price: 90 },
  { quantity: 'Pulses', price: 200 },
]);
  
const output = input.whereBetween('price', [90, 150]);
console.log(output.all());

Output:

[ { quantity: 'Flour', price: 150 },
  { quantity: 'Rice', price: 100 },
  { quantity: 'Fruits', price: 90 } ]

Reference: https://collect.js.org/api/whereBetween.html




My Personal Notes arrow_drop_up
Recommended Articles
Page :