当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Collect.js whereInstanceOf()用法及代码示例

whereInstanceOf()函数用于过滤具有给定类类型的输入。在JavaScript中,首先将数组转换为一个集合,然后将该函数应用于该集合。

用法:

data.whereInstanceOf('key')

参数:该函数接受如上所述和以下描述的单个参数:

  • Key:此参数包含定义该键的值的键名称。

返回值:返回具有提到的键值的集合。
下面的示例说明了collect.js中的whereInstanceOf()函数。示例1:在此示例中,我们获取一个集合,然后使用whereInstanceOf()方法使用键返回过滤的集合。

Javascript



// It is used to import collect.js library 
const collect = require('collect.js'); 
  
const Input = collect([ 
  new Books('Pride and prejudice'), 
  new Books('The Great Gatsby '), 
  new Movies('It'), 
]); 
  
const output = Input.whereInstanceOf(Books); 
console.log(output.all());

输出:

[
   new Books('Pride and prejudice'),
   new Books('The Great Gatsby'),
]

范例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 Name('Mohan'), 
]); 
  
const output = Input.whereInstanceOf(Name); 
  
console.log(output.all());

输出:

[
  new Name('Mohan'),
]

参考文献:https://collect.js.org/api/whereinstanceof.html

相关用法


注:本文由纯净天空筛选整理自akhilsharma870大神的英文原创作品 Collect.js whereInstanceOf() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。