HomeLanguagesJavascriptCollect.js whereIn() Function

Collect.js whereIn() Function

The whereIn() function is used to filter the collection by a given key or value contained within the given array. In JavaScript, the array is first converted to a collection and then the function is applied to the collection.

Syntax:

data.whereIn('key')

Parameters: This function accepts a single parameter as mentioned above and described below:

  • Key: This parameter holds the key name that defines the value of that key.

Return value: Returns the collection with a key value that was mentioned.

Below examples illustrate the whereIn() function in collect.js

Example 1: Here in this example, we take a collection and then using the whereIn() method we have returned filtered collection using the key.

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.whereIn('price', [1000, 1500]);
 
filtered.all();


Output:

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

Example 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.whereIn('Year',[2000, 2025]);
 
console.log(output.all());


Output:

[
  new Year('2025')
]

Reference: https://collect.js.org/api/whereIn.html

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32561 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6936 POSTS0 COMMENTS
Nicole Veronica
12056 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12174 POSTS0 COMMENTS
Shaida Kate Naidoo
7075 POSTS0 COMMENTS
Ted Musemwa
7318 POSTS0 COMMENTS
Thapelo Manthata
7028 POSTS0 COMMENTS
Umr Jansen
7020 POSTS0 COMMENTS