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


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

collect.js中的whereNotIn()方法用於根據鍵和值過濾給定集合中的元素。如果找到特定的鍵值集,則會將其過濾掉。

安裝:

  • 在NodeJs中:
    npm install collect.js
  • 用於collect.js的CDN
    <script src="https://cdnjs.com/libraries/collect.js"></script>

用法:

whereNotIn(key, array_value);

參數:

  • key:要刪除其值的 key 。
  • array_value:該鍵將要刪除的值的數組。

返回值:它返回對象。



範例1:

Javascript

// Importing the collect.js module. 
const collect = require('collect.js'); 
let obj1 = [ 
    { "a":1 }, 
    { "a":2 }, 
    { "a":3 }, 
    { "a":4 }, 
    { "b":5 } 
] 
  
// Making a collection 
let collection = collect(obj1); 
  
// Using whereNotIn() method to return 
// a collection not having value 2, 4 
// For key "a" 
let collectionFilter = collection 
            .whereNotIn("a", [2, 4]); 
  
console.log("Original collection is:", 
            collection.all()) 
              
console.log("Filtered collection is:",  
            collectionFilter.all());

輸出:

範例2:

Javascript

// Importing the collect.js module. 
const collect = require('collect.js'); 
  
let obj1 = [ 
    { "b":1 }, 
    { "c":2 }, 
    { "b":3 }, 
    { "b":4 }, 
    { "b":5 }, 
    { "c":11 }, 
    { "c":12 }, 
] 
  
// Making a collection 
let collection = collect(obj1); 
  
// Using whereNotIn() method to return  
// a collection not having value 1, 2, 4 
// For key "b" 
let collectionFilter =  
    collection.whereNotIn("b", [1, 2, 4]); 
  
collectionFilter =  
    collection.whereNotIn("c", [11]); 
  
console.log("Original collection is:",  
    collection.all()); 
  
console.log("The output will not contain "
        + "value of 1, 2, 4 for key \"b\""
        + " and value of 11 for key \"c\""); 
  
console.log("Filtered collection is:",  
        collectionFilter.all());

輸出:

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

相關用法


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