Skip to content Skip to sidebar Skip to footer

How to Check if Value Shows Up Again in Array

Four Different Ways to Search an Array in JavaScript

There are different methods in JavaScript that you tin can use to search for an item in an assortment. Which method you choose depends on your specific use example.

For case, do y'all want to become all items in an array that come across a specific condition? Practise you want to cheque if any item meets the condition? Do y'all want to cheque if a specific value is in the assortment? Or practise yous want to notice the index of the value in the array?

For all these employ cases, JavaScript'southward Array.prototype methods accept you covered. In this article, we volition talk over four methods we can use to search for an particular in an array. These methods are:

  1. Filter
  2. Observe
  3. Includes
  4. IndexOf

Permit'south discuss each of them.

Array.filter()

Nosotros can apply the Array.filter() method to find elements in an array that see a certain status. For instance, if we desire to get all items in an assortment of numbers that are greater than 10, we can do this:

                const array = [ten, xi, iii, twenty, v];  const greaterThanTen = array.filter(element => element > 10);  console.log(greaterThanTen) //[11, 20]              

The syntax for using the array.filter() method is the following:

                let newArray = assortment.filter(callback);              

where

  • newArray is the new array that is returned
  • array is the array on which the filter method is chosen
  • callback is the callback function that is applied to each element of the array

If no item in the array meets the condition, an empty array is returned. You can read more than most this method here.

There are times when we don't need all the elements that come across a certain status. We just demand ane chemical element that matches the condition. In that instance, you need the notice() method.

Array.find()

Nosotros utilize the Assortment.find() method to discover the get-go element that meets a certain condition. Just like the filter method, it takes a callback as an argument and returns the beginning chemical element that meets the callback condition.

Let's use the detect method on the array in our case above.

                const array = [10, 11, three, 20, v];  const greaterThanTen = array.discover(element => element > 10);  console.log(greaterThanTen)//11              

The syntax for the array.find() is

                allow element = array.find(callback);              

The callback is the part that is executed on each value in the array and takes three arguments:

  • element - the element beingness iterated on (required)
  • index - the index/position of the current element (optional)
  • array - the array that find was called on (optional)

Note, though, that if no item in the array meets the condition, it returns undefined.

What if, though, you want to check if a certain element is in an assortment? How do you lot do this?

Array.includes()

The includes() method determines whether an array includes a certain value and returns true or false as advisable.

Then in the example above, if nosotros want to bank check if 20 is one of the elements in the array, we tin practice this:

                const assortment = [10, eleven, iii, xx, 5];  const includesTwenty = assortment.includes(20);  console.log(includesTwenty)//true              

You'll notice a difference between this method and other methods we have considered. This method accepts a value rather than a callback as the argument. Here's the syntax for the includes method:

                const includesValue = array.includes(valueToFind, fromIndex)              

Where

  • valueToFind is the value you are checking for in the array (required), and
  • fromIndex is the index or position in the assortment that you desire to start searching for the element from (optional)

To get the concept of the index, let's visit our example again. If we want to bank check whether the assortment contains 10 in other positions autonomously from the first element, nosotros can do this:

                const array = [10, 11, iii, 20, 5];  const includesTenTwice = array.includes(10, 1);  console.log(includesTenTwice)//fake              

Array.indexOf()

The indexOf() method returns the first index at which a given element can be constitute in an array. It returns -one if the chemical element does not exist in the array.

Allow's become back to our case. Let's detect the index of iii in the assortment.

                const array = [x, eleven, 3, 20, 5];  const indexOfThree = array.indexOf(3);  console.log(indexOfThree)//two              

Its syntax is similar to that of the includes method.

                const indexOfElement = array.indexOf(chemical element, fromIndex)              

Where

  • element is the element you are checking for in the assortment (required), and
  • fromIndex is the index or position in the array that y'all desire to starting time searching for the element from (optional)

It's of import to note that both the includes and indexOf methods use strict equality( '===' ) to search the array. If the values are of different types (for example '4' and 4), they'll return false and -ane respectively.

Summary

With these array methods, you don't need to use a for loop to search an assortment. Depending on what yous need, you tin can make up one's mind which of the methods is best suited for your utilise case.

Here is a summary of when to employ each method:

  • Utilize filter if y'all desire to find all items in an array that meet a specific condition.
  • Use notice if you desire to cheque if that at least one detail meets a specific condition.
  • Use includes if you want to check if an array contains a particular value.
  • Employ indexOf if you lot want to detect the index of a item detail in an assortment.

Desire to get notified when I publish a new article? Click hither.



Learn to code for complimentary. freeCodeCamp's open source curriculum has helped more than xl,000 people get jobs as developers. Get started

meltonaguire.blogspot.com

Source: https://www.freecodecamp.org/news/4-methods-to-search-an-array/

Post a Comment for "How to Check if Value Shows Up Again in Array"