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


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

where()函數用於通過給定數組中包含的給定鍵或值來過濾集合。在JavaScript中,首先將數組轉換為一個集合,然後將該函數應用於該集合。

用法:

data.where('key')

參數:該函數接受如上所述和以下描述的單個參數:

  • Key:此參數包含定義該鍵的值的鍵名稱。

返回值:返回具有提到的鍵值的集合。

以下示例說明了collect.js中的where()函數:



範例1:在此示例中,我們獲取一個集合,然後使用where()方法使用鍵返回過濾後的集合。

Javascript

// It is used to import collect.js library
 
const collection = collect([
  { Book:'Let US C', price:2000 },
  { Book:'Begin Python', price:1000 },
  { Book:'Learn the DEV', price:1500 },
]);
 
const filtered = collection.where('price', [1000, 1500]);
filtered.all();

輸出:

[
 { Book:'Begin Python', price:1000 },
 { Book:'Learn the DEV', price:1500 }
]

範例2:

Javascript

// It is used to import collect.js library 
const collect = require('collect.js'); 
 
const Input = collect([ 
  new Year('1980'), 
  new Year('2020'), 
  new Year('2025'), 
]); 
 
const output = Input.where('Year',[2000, 2025]); 
 
console.log(output.all());

輸出:

[
 new Year('2025')
]

參考:https://collect.js.org/api/where.html

相關用法


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