given an array of objects
l = [a,b,c,d]
l.map(function(x){x.zop({foo:bar})})
is this a bad idea?
i mean it works and all 😀
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, it is perfectly fine to use array iteration functions and I personaly try to use them whenever I can instead of a for loop. The main reasons I do so are:
No need to declare useless variables (like the loop counter) that clutter the code and confuse my JSHint.
I need to write down the array only once. This makes it easier to rename the array variable if I want to and it is also more amenable to passing an expression directly (instead of having to save the array in aother extraneous variable)
Regular for loops are vulnerable to the closures-inside-for-loops bug.
Do remember though that
map,forEachand friends are not present in older browsers (particularly, Internet Explorer) so you should either use a shim (to add these methods to the native arrays) or use similar functions from a custom library.edit: nowadays you should feel free to use these functions. If you still need to support IE8 you have bigger problems than this…