By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > Uncategorized > In JavaScript, Searching an Array For a Value
Uncategorized

In JavaScript, Searching an Array For a Value

Admin
Last updated: 2023/01/23 at 5:40 AM
Admin
Share
4 Min Read
Searching an Array For a Value

In this article, we are going to talk about how we can perform searching an array for a value. Searching an array is a common task. In the past, we have relied on indexOf() and lastIndexOf(). As of ES6, there are two new array methods: findIndex() and find(). This tutorial covers indexOf(), lastIndexOf(), findIndex() and find().

Contents
Searching an Array For a Value: indexOf() and lastIndexOf()Searching an Array For a Value: findIndex()Searching an Array For a Value: find()You may also like:

So as we know, we can do searching an array operation for a value using indexOf() and lastIndexOf() methods which returns index of given value. But it becomes hard, when we try to find a number of elements from a given array on basis of certain conditions, then it becomes necessary to loop or another array to do this kind of operation. But today, we will another ways to do this same operation.

Searching an Array For a Value: indexOf() and lastIndexOf()

Let’s see, first, how we can use indexOf() and lastIndexOf() methods. Here’s a basic example for this, in here we have defined an array with some value, and also we added a duplicate value. Now we can use indexOf() method to find value 38 and this method will return the very first index of this value. And we will use lastIndexOf() method to find the same value, but this method will return the very last index of the value.

>let arr = [1, 20, 38, 7, 27 ,38]
>undefined
>arr.indexOf(38)
>2
>arr.lastIndexOf(38)
>5

Searching an Array For a Value: findIndex()

In findIndex() method, we have to pass a function where we add some logic to get some filtered values from the array. In this function we have passed three parameters, element, index and new array. In element, the value of the array will be assigned every time, index will be the index number of that element and new array will target the given array, when we will call this function inside findIndex() method to apply on array.

Now in this function we have added a logic to get values until value is less than 30, and we are just displaying those elements. Here we will also get the index of last element.

let arr = [1, 27, 6, 30 , 56, 2, 30];
let over30 = function(ele, index, theArr){
      console.log(ele + "-" + index + "-" + theArr);
      return ele > 30;
}
console.log(arr.findIndex(over30));
Searching an Array For a Value

Searching an Array For a Value: find()

The find() method is most of similar to findIndex() method, which again takes a function as a parameter and this function contains similar three parameters. But here is the only difference between these two method is findIndex() returns index of last value inputted in the array, where find() method will return value of last element added to array.

let arr = [1, 27, 6, 30 , 56, 2, 30];
let over30 = function(ele, index, theArr){
       console.log(ele + "-" + index + "-" + theArr);
       return ele > 30;
}
console.log(arr.find(over30));
Searching an Array For a Value

You may also like:

  • How to Define a Class With Properties and Methods in JavaScript?
  • How to Implement Class Inheritance in JavaScript?
  • How to Find Duplicate Elements in a Given Array?

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

TAGGED: javascript, Searching an Array For a Value, Searching an Array For a Value javascript
Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Admin
Follow:
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article Extracting Numbers From a String Extracting Numbers From a String in JavaScript
Next Article Formatting Time Using The Date Object and String Padding Formatting Time Using The Date Object and String Padding
Leave a comment Leave a comment

Leave a Reply Cancel reply

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

- Advertisement -

You Might Also Like

Responding to and Tracking Key Presses

How to Work With Responding to and Tracking Key Presses

February 5, 2023
Passing a JavaScript Value Between HTML Pages

Passing a JavaScript Value Between HTML Pages

February 3, 2023
Compare Objects in an Array

JavaScript Problem: Compare Objects in an Array

January 30, 2023
Switching Name Order Using Capturing Groups in Regular Expressions

Switching Name Order Using Capturing Groups in Regular Expressions

January 29, 2023
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?