šŸ“• Build AI Agents using LangGraph.js is now out!

Javascript includes() multiple values

Ever found yourself needing to check multiple items in a JavaScript array at once? While the includes() function is powerful, it doesn't natively support multiple values. But don't worry! With a few clever tricks, you can easily extend its functionality to handle multiple conditions.

The Javascript array includes() function will return true if a value is found in an array or false otherwise.

But is it possible to add multiple conditions to this function? Something like this:

myArray.includes("šŸŽ apple", "šŸ pear", "🌽 corn")

Unfortunately, the includes() function does not have a multiple values search option, but we can simulate this with the help of others Javascript array functions.

Check for at least one value to be included in an array

Let's say we want to test if AT LEAST ONE value, from a given set, is present in a Javascript array.

We can do this by mixing the includes() function and the some() function:

//1. checking if AT LEAST ONE value is in the array
const products = ['šŸŽ apple', '🄬 kale', 'šŸ pear', '🌽 corn']
const fruits = ['šŸ pear', 'šŸŽ apple']
const veggies = ['🄬 kale', 'šŸ§… onion']

const atLeastOneFruit = fruits.some(i => products.includes(i))
// true
const atLeastOneVeggie = veggies.some(i => products.includes(i))
// true

Check for all values to be included in an array

On the other hand, if we want to check if ALL values are present in a Javascript array, we will use includes() combined with the every() method:

//2. checking if ALL THE values are in the array
const products = ['šŸŽ apple', '🄬 kale', 'šŸ pear', '🌽 corn']
const fruits = ['šŸ pear', 'šŸŽ apple']
const veggies = ['🄬 kale', 'šŸ§… onion']

const allTheFruits = fruits.every(i => products.includes(i))
// true
const allTheVeggies = veggies.every(i => products.includes(i))
// false, the 'šŸ§… onion' is missing

You can checkout the full code of this example on my Gitub and the running app here.

Notes on the Javascript includes() function

Some extra notes on this function:

  • the include function is case-sensitive. For example ["rob", "joe"].includes("ROB") will return false;
  • also, include() takes a second optional argument. This argument is the index from where to start the search from. For example myArray.includes("Rob", 2) will return true if the "Rob" value is found only after the index 2 of the array. The index is zero based.

And there you have it! Whether you need to find one or all items from a JavaScript array, you can use one of these methods. Happy coding!

šŸ“– Build a full trivia game app with LangChain

Learn by doing with this FREE ebook! This 35-page guide walks you through every step of building your first fully functional AI-powered app using JavaScript and LangChain.js

šŸ“– Build a full trivia game app with LangChain

Learn by doing with this FREE ebook! This 35-page guide walks you through every step of building your first fully functional AI-powered app using JavaScript and LangChain.js


Leave a Reply

Your email address will not be published. Required fields are marked *