當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Collect.js whereBetween()用法及代碼示例

whereBetween()函數用於過濾給定範圍內的輸入。在JavaScript中,首先將數組轉換為一個集合,然後將該函數應用於該集合。語法:

data.whereBetween(key, [range]);

參數:該函數接受上麵提到並在下麵描述的兩個參數:

  • Key:此參數保存定義該鍵值的鍵名。
  • Range:指定範圍

返回值:返回範圍內的過濾後的集合。

下麵的示例說明了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());

輸出:

[ { 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

相關用法


注:本文由純淨天空篩選整理自akhilsharma870大神的英文原創作品 Collect.js | whereBetween() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。