javascript - Is there a way to obtain index in array in more concise way? -
curiosity sponsors question, it's fun hack in languages.
assume there array holding objects of kind. objects have id property. id cannot empty string, false, null or falsey value. array can of 0 length. want obtain index of value (if present) , exit loop it's found.
var id = /some value check/; var index; (var = 0, item; item = arr[i], item===undefined?false:item.id==id?(index=i,false):true; i++);
what think?
meanwhile did jsperf posted above suggestions. please play around if want.
fastest initial example above under ff , while
loop posted in comments under chrome. find
solution arrow functions slowest in both cases. cannot test in ie use no win.
i'm aware jsperf badly written - please play around.
Comments
Post a Comment