7 Ways to Find in array using JavaScript

7 ways to find in array using JavaScript | Code Architects

There are many different ways to find value in an array. We will discuss few pre-defined ways to find value from an array using JavaScript. They are:

1. find()

The array find() method returns the first matched element in an array that satisfies a condition. The find() method takes a function as an argument that returns true or false based on some condition.

If it finds an array element where the function returns a true value, find() returns the value of that array element otherwise it returns undefined.

let score = [55,77,82,66,48];
let value = score.find(val => val > 60);
console.log(value);

// 77
2. findIndex()

The array findIndex() method returns the index of the first matched element in the array that satisfies a condition. This method returns the index of the first element in an array that passes a test.

If it finds an array element where the function returns a true value, findIndex() returns the index of that array element otherwise it returns -1.

let score = [55,77,82,66,48];
let value = score.findIndex(val => val > 60);
console.log(value);
// 1
3. includes()

The includes() method checks if a specified element is present or not in an array and returns a Boolean value.

This method returns true if the array contains the element, and false if not.

let menu = ["Pizza", "Burger", "Momos"];
console.log(menu.includes("Dal-baati"));
// false
4. indexOf()

This method returns the index of the first occurrence of matched elements in an array. If no element is matched then indexOf returns -1. This method is case sensitive.

let fruits = ["๐Ÿ“", "๐Ÿ’", "๐ŸŽ", "๐Ÿ‰"];
console.log(fruits.indexOf("๐Ÿ“"));   // 0
console.log(fruits.indexOf("๐Ÿ"));   // -1

Read our other blog posts..

5. lastIndexOf()

The method returns the index of the last occurrence of the matched elements in an array. If the item to search for is present more than once, the lastIndexOf() method returns the position of the last occurrence. It searches elements from the end to the beginning of an array. If no element is matched then it returns -1. This method is also case sensitive.

let fruits = ["๐ŸŽ", "๐Ÿ’", "๐ŸŽ", "๐Ÿ‰"];
console.log(fruits.indexOf("๐Ÿ"));  //-1
console.log(fruits.indexOf("๐ŸŽ"));  // 2
6. map()

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array. The map() method calls the provided function once for each element in an array, in order.

let marks = [53,29,65,22,71];
let pass = [];
marks.map(val => {
  if(val >= 30) pass.push(val);
});
7. filter()

This method finds and filters out elements that do not pass the condition. This method takes a function as an argument that returns true or false. This function executes for each element in array. If the function returns true for any element then only that element gets included in the returned array.

let marks = [53,29,65,22,71];
let pass = marks.filter(val => val >= 30);
console.log(pass);
// [53,65,71]

These were few ways to find a value in an array with JavaScript. Read our other blogs here.

Show 28 Comments

28 Comments

  1. Random video chat app! Click! Perfect random video chat! Oralla Amble Viehmann

  2. Good article! We are linking to this great post on our website. Keep up the good writing. Ceil Tallie Holbrook Alyss Briggs Kathryn

  3. Quod autem in homine praestantissimum atque optimum est, id deseruit. Apud ceteros autem philosophos, qui quaesivit aliquid, tacet; Sedulo, inquam, faciam. Naturales divitias dixit parabiles esse, quod parvo esset natura contenta. Suo enim quisque studio maxime ducitur. Sibby Spike Mattland

  4. Pretty! This has been a really wonderful article. Thank you for supplying these details. Rosita Allen Goldi

  5. Simply want to say your article is as amazing. The clarity
    in your submit is just cool and i could suppose you are an expert on this subject.
    Fine together with your permission allow
    me to seize your RSS feed to keep updated with forthcoming post.

    Thanks one million and please keep up the rewarding work.

  6. Very descriptive article, I loved that bit. Will there be
    a part 2?

  7. instgaram tรผrk takipรงi satฤฑn al

    Pretty nice post. I simply stumbled upon your weblog and wished to say that I have really
    enjoyed surfing around your weblog posts.
    After all I’ll be subscribing on your feed and I am hoping you write again soon!

    • Honour, We will soon consider starting the subscription of our blog. ๐Ÿ˜Š
      // Until then you can use the bell icon to turn the notifications

  8. Blessphemy Guitar Solo

    Very good info.
    Lucky me I came across your blog by accident (stumbleupon).

    I’ve saved as a favorite
    for later!

  9. Haberler

    Greetings! I know this is somewhat off topic but I was wondering if you knew
    where I could locate a captcha plugin for my comment form?
    Iโ€™m using the same blog platform as yours and Iโ€™m having trouble finding
    one? Thanks a lot.!

  10. ilahi sรถzleri

    My relatives every time say that I am wasting my time here at net, however I
    know I am getting know-how all the time by reading thes
    good articles.

  11. Numara Sorgula

    Thank you for this beautiful article. It’s really a good article

    • Thanks for the feedback ๐Ÿ˜Šโค
      Do check out our other blog posts too โœŒ

  12. Whitney

    Hi there just wanted to give you a brief heads up and let you know a few of the images aren’t loading properly. I’m not sure why but I think its a linking issue. I’ve tried it in two different web browsers and both show the same outcome.

    • Hello Whitney
      The website is updated. The images are loading up properly.
      Thank you for informing ๐Ÿ™‚

  13. Sheldon Joiner

    Hello mates, nice paragraph and pleasant urging commented
    here, I am truly enjoying by these.

  14. Sohbet Odalarฤฑ

    First of all, thank you for writing such an article. every word is full of knowledge

Comments are closed