🎁 The Js-Craft Guide to React is now available with a 30% off discount!

Javascript includes() multiple values

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 side, 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 include() 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.

10 Javascript AI projects with Langchain & React

Not sure what to build? Dive in with these Javascript AI projects! Learn how to build LLM powered apps using Langchain.js and React! Connect your apps to LLMs such as OpenAI, create agents, use vector databases, or setup AI context.

10 Javascript AI projects with Langchain & React

Not sure what to build? Dive in with these Javascript AI projects! Learn how to build LLM powered apps using Langchain.js and React! Connect your apps to LLMs such as OpenAI, create agents, use vector databases, or setup AI context.


Leave a Reply

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

📘 The Guide to React Home Screencasts Best of Newsletter Search X

📘 - 10 Javascript AI projects with React and LangChain

Hi friend! Before you go, just wanted to let you know about the 10 Javascript AI projects ideas with React and LangChain FREE ebook.

One of the best ways to learn is by doing the work. Choose from these 10 project ideas and start working on topics such as:

  • How does LangChain work
  • Connecting to OpenAI LLM
  • Create AI Agents
  • Simple and Sequential Chains
  • Adding Memory (Chat Context)
  • Prompt Templates
  • Using OutputParsers and Tools for Agents
  • Work with Documents and more...

Keep building and see the new capabilities LLM models can add to your React app! Get all projects as an ebook right to your inbox!

X