This is a interesting error i found when developing a simple feature in javascript. Requirement was very simple, loop through every object in an array and match their ids. I implemented the solution using array.find() method.
javascript array.find() takes a function as an argument and passes "element, index, array" as parameters. I thought this is very handy and can be used. I completed the feature, tested in Firefox browser. Everything worked well.
But an issue was reported saying, feature not working. Then i inspected, everything looked fine until i asked this question "which browser?" to my tester. He replied "Chrome", Ah..... gotcha!! When i tested in chrome, there was a javascript error saying no method "find()" found in object array.
After doing a little bit search and research in google, it was clear to my understanding that "find()" is supported only in Firefox, if we have to support different browser then i have to use "forEach()" method.
Interesting fact is, "forEach()" and "find()" are similar in functionality. They both have same method signatures and does same job. Below is the details of both the functions.
forEach()
Syntax :
arr.forEach(callback[, thisArg])
Parameters :
callback : Function to execute for each element.
thisArg : Value to use as this when executing callback.
Usage eg :
Syntax :
arr.find(callback[, thisArg])
Parameters :
callback : Function to execute for each element.
thisArg : Value to use as this when executing callback.
Usage eg :
javascript array.find() takes a function as an argument and passes "element, index, array" as parameters. I thought this is very handy and can be used. I completed the feature, tested in Firefox browser. Everything worked well.
But an issue was reported saying, feature not working. Then i inspected, everything looked fine until i asked this question "which browser?" to my tester. He replied "Chrome", Ah..... gotcha!! When i tested in chrome, there was a javascript error saying no method "find()" found in object array.
After doing a little bit search and research in google, it was clear to my understanding that "find()" is supported only in Firefox, if we have to support different browser then i have to use "forEach()" method.
Interesting fact is, "forEach()" and "find()" are similar in functionality. They both have same method signatures and does same job. Below is the details of both the functions.
forEach()
Syntax :
arr.forEach(callback[, thisArg])
Parameters :
callback : Function to execute for each element.
thisArg : Value to use as this when executing callback.
Usage eg :
function logArrayElements(element, index, array) {
console.log("a[" + index + "] = " + element);
}
[2, 5, 9].forEach(logArrayElements);
// logs:
// a[0] = 2
// a[1] = 5
// a[2] = 9
console.log("a[" + index + "] = " + element);
}
[2, 5, 9].forEach(logArrayElements);
// logs:
// a[0] = 2
// a[1] = 5
// a[2] = 9
find()
Syntax :
arr.find(callback[, thisArg])
Parameters :
callback : Function to execute for each element.
thisArg : Value to use as this when executing callback.
Usage eg :
function logArrayElements(element, index, array) {
console.log("a[" + index + "] = " + element);
}
[2, 5, 9].find(logArrayElements);
// logs:
// a[0] = 2
// a[1] = 5
// a[2] = 9
console.log("a[" + index + "] = " + element);
}
[2, 5, 9].find(logArrayElements);
// logs:
// a[0] = 2
// a[1] = 5
// a[2] = 9
No comments:
Post a Comment