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 :-
Top 10 Trending GitHub Repositories
Pawan Bhayde ・ Apr 23 ・ 4 min read
This content originally appeared on DEV Community and was authored by Pawan Bhayde
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.