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..
- https://teamcodearchitects.com/blog/create-different-shapes-with-css/
- https://teamcodearchitects.com/blog/css-selector-cheatsheet/
- https://teamcodearchitects.com/blog/7-best-javascript-video-plugins-for-your-next-project/
- https://teamcodearchitects.com/blog/glassmorphism-design/
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.
Random video chat app! Click! Perfect random video chat! Oralla Amble Viehmann
Good article! We are linking to this great post on our website. Keep up the good writing. Ceil Tallie Holbrook Alyss Briggs Kathryn
Thank you & Keep up the support! ๐โ
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
Pretty! This has been a really wonderful article. Thank you for supplying these details. Rosita Allen Goldi
You are welcome๐
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.
Thank You๐๐ฅ
Wow, that’s what I was seeking for, what a stuff!
existing here at this webpage, thanks admin of this web
page.
Your welcome ๐
Very descriptive article, I loved that bit. Will there be
a part 2?
Yes, soon probably
Thank you for visiting ๐
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
Thank you for your feedback. ๐
Very good info.
Lucky me I came across your blog by accident (stumbleupon).
I’ve saved as a favorite
for later!
Thank you for the feedback ๐
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.!
I am honestly not aware of it …
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.
Thank you for this beautiful article. It’s really a good article
Thanks for the feedback ๐โค
Do check out our other blog posts too โ
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 ๐
Hello mates, nice paragraph and pleasant urging commented
here, I am truly enjoying by these.
Thank you ๐
First of all, thank you for writing such an article. every word is full of knowledge
๐๐