收集.js | whereBetween()函数
whereBetween()函数用于过滤给定范围内的输入。在 JavaScript 中,首先将数组转换为集合,然后将函数应用于集合。
句法:
data.whereBetween(key, [range]);
参数:该函数接受上面提到的两个参数,如下所述:
- Key:此参数保存定义该键值的键名。
- 范围:指定范围
返回值:返回范围内过滤的集合。
下面的示例说明了 collect.js 中的whereBetween()函数:
示例 1:在此示例中,我们获取一个集合,然后使用whereBetween()方法指定一个键和值范围,以检查值并返回该范围内的值。
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());
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());
输出:
[ { fruits: 'Apple', price: 200 },
{ fruits: 'Papaya', price: 150 },
{ fruits: 'Cherry', price: 100 }]
示例 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());
输出:
[ { quantity: 'Flour', price: 150 },
{ quantity: 'Rice', price: 100 },
{ quantity: 'Fruits', price: 90 } ]
参考: https ://collect.js.org/api/whereBetween.html