7 Way To Find In Array Using Javascript

find()

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

let score = [55,77,82,66,48];
let va…


This content originally appeared on DEV Community and was authored by Pawan Bhayde

find()

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

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

findlndex()

The Array findindex() method returns the index of first matched element in array that satisfies a condition. This method also takes a function argument which returns true or false.

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

includes()

The method checks if a specified element is present or not in an array and returns a Boolean value. If the specified element is
present in array then it returns true else false. This method is case sensitive.

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

indexOf()

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

let fruits = ["?","?","?","?"];
console.log(fruits.indexOf("?")); // 0
console.log(fruits.indexOf("?")); 11-1

lastIndexOf()

The method returns the index of last occurrence of matched element in array. It searches element from end to beginning of array. If no element is matched then it returns -1. This method is case sensitive.

let fruits = ["?","?","?","?"];
console.log(fruits.lastIndexOf("?"));  //-1
console.log(fruist.lastIndexOf("?"));  //2

map()

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

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

filter()

This method finds and filter out elements that do not pass the condition. This method takes a function as argument which 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]

Note:- visit my youtube channel for frontend tutorials

subscribe to my youtube channel :- https://www.youtube.com/channel/UCR64vQptythbJ1SmI-ub0Rg

Resent post :-


This content originally appeared on DEV Community and was authored by Pawan Bhayde


Print Share Comment Cite Upload Translate Updates
APA

Pawan Bhayde | Sciencx (2021-04-24T13:18:28+00:00) 7 Way To Find In Array Using Javascript. Retrieved from https://www.scien.cx/2021/04/24/7-way-to-find-in-array-using-javascript/

MLA
" » 7 Way To Find In Array Using Javascript." Pawan Bhayde | Sciencx - Saturday April 24, 2021, https://www.scien.cx/2021/04/24/7-way-to-find-in-array-using-javascript/
HARVARD
Pawan Bhayde | Sciencx Saturday April 24, 2021 » 7 Way To Find In Array Using Javascript., viewed ,<https://www.scien.cx/2021/04/24/7-way-to-find-in-array-using-javascript/>
VANCOUVER
Pawan Bhayde | Sciencx - » 7 Way To Find In Array Using Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/24/7-way-to-find-in-array-using-javascript/
CHICAGO
" » 7 Way To Find In Array Using Javascript." Pawan Bhayde | Sciencx - Accessed . https://www.scien.cx/2021/04/24/7-way-to-find-in-array-using-javascript/
IEEE
" » 7 Way To Find In Array Using Javascript." Pawan Bhayde | Sciencx [Online]. Available: https://www.scien.cx/2021/04/24/7-way-to-find-in-array-using-javascript/. [Accessed: ]
rf:citation
» 7 Way To Find In Array Using Javascript | Pawan Bhayde | Sciencx | https://www.scien.cx/2021/04/24/7-way-to-find-in-array-using-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.