Array search and filter array from javascript
Array search and filter array from javascript
Prototype is a object, and every javascript object have inbuild properties called prototype.1. indexOf( )
Return the first (least ) index of an element within the array equal to an element, or -1 if none is found. It search the element from the 0th index number.
Array.prototype.indexOf()
var arr = ["shreyash", "kolhe","vaibhav", "amit"];console.log(arr.indexOf("kolhe"));
Output
1
2. lastIndexOf()
Returns the last (Greatest) index of an element within the array equal to an element, or -1 if none is found. it search the element last to first
Array.prototype.lastIndexOf()
var arr = ["shreyash", "kolhe","vaibhav", "amit"];console.log(arr.lastIndexOf("kolhe"));
Output
3
3. includes()
Determines whether the array contains a value, returning true of false as appropriate.
Array.prototype.includes()
var arr = ["shreyash", "kolhe","vaibhav", "amit"];console.log(arr.includes("kolhe"));
Output
True
4.find
Returns the found element in the array, if some element in the array satisfies the testing function, or undefined if not found. only problem is that it return only one element.
Array.prototype.find()
const prices = [200, 300 , 523, 255, 122, 632];const findElem = prices.find((element)=>{return element> 300;});console.log(findElem);
OUtput
200
5.findIndex()
Returns the found index in the array, if an element in the array satisfies the testing function, or -1 if not found.
Array.prototype.findIndex()
const prices = [200, 300 , 523, 255, 122, 632];const findElem = prices.findIndex((element)=>{return element> 300;});console.log(findElem);
OUtput
200
0
Post a Comment