r/javascript • u/varun_raj • Aug 08 '16
A collection of most commonly used functions in modern webapps
https://www.npmjs.com/package/common-lib
0
Upvotes
5
u/sbruchmann Aug 08 '16
module.exports.getElementIndex = function(id, array) {
for (var i = 0; i < array.length; i++) {
if (array[i].id == id) {
return i;
}
}
return -1;
}ugh…
const getElementIndexById = (els, id) => els.findIndex(el => el.id === id)
2
2
u/guywithalamename Aug 08 '16 edited Aug 08 '16
Half of this is already backed into the language.. I mean getElementIndex? seriously?
edit: Turns out the function does something completely different than the docs say it does..
-3
6
u/prawnsalad Aug 08 '16
An entire function for this? Does
mystring[0]not work well enough? Even if you did prefercharAtover indexed access,mystrings.charAt(0)doesn't need a wrapper.