I regularly use for
loop , forEach
loop , Array.map
etc for iterating arrays. i came to know that we can also use Array.find
for looping array and it can also return the values matched in the array based on condition:
I have iterated array of objects using Array.find()
and for
loop as follows
Using Array.find:
[{
name: "John"
}, {
name: "Cannady"
}, {
name: "Sherlock"
}].find(function(oArrayObject) {
console.log(oArrayObject)
});
output on console:
{name: "John"}
{name: "Cannady"}
{name: "Sherlock"}
Using for loop:
var aNames=[{
name: "John"
}, {
name: "Cannady"
}, {
name: "Sherlock"
}]
for(var i=-0;i<aNames.length;i++){
console.log(aNames[i]);
}
output on console:
{name: "John"}
{name: "Cannady"}
{name: "Sherlock"}
Both codes did the same thing.
What is the advantage of Array.find() over for loop??
Which one gives the better performance ??
Please explain me the difference between array.find and other iterators in terms of performance , usage , internal functionality etc...