Array Function: find() and findIndex()
ES6 introduced 2 new methods to search for elements inside the
Array. ES6 is different version of the
ECMAScript specification. Which is the standard that defines the
scripting language that javascript is based on.
ES6 release on 2015 and there are so many features Arrow function,
classes, let and const for variables.
find()
- It is used to search for an element in the array that matches some
condition. It returns the first element that matches the condition.
findIndex() - It is quite similar to the find()
method. The difference is that findIndex() method returns the index of the
element instead of the element itself.
Example
Here written code says that if our condition are matches or true
condition then it returns the first element. If you want in replace of return
true you can write any if, else condition or write even or odd conditon.
const numArr = [-1, -2, -3, -4, 1, 2, 3, 4] const result = numArr.find(item => { return true; }) console.log('Result => ', result);
Output
Example (return False)
if we return false statement then output is undefined...
Example
Let's take another example of even - odd condition for returning the matches
array elements.
return item % 2 === 0;
Disclaimer
All tutorials are for informational and educational
purposes only and have been made using our own routers, servers, websites and
other vulnerable free resources. we do not contain any illegal activity. We
believe that ethical hacking, information security and cyber security should
be familiar subjects to anyone using digital information and computers.
Hacking Truth is against misuse of the information and we strongly suggest
against it. Please regard the word hacking as ethical hacking or penetration
testing every time this word is used. We do not promote, encourage, support or
excite any illegal activity or hacking.
0 comments:
Post a Comment
For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.