Pluck enumeration itt

11 views
Skip to first unread message

kstubs

unread,
Aug 23, 2011, 10:58:26 PM8/23/11
to prototype-s...@googlegroups.com
How do you pluck the enumeration itt value?  I need to find an item within an enumeration, but I just need it's index.

Karl..

Walter Lee Davis

unread,
Aug 23, 2011, 11:46:59 PM8/23/11
to prototype-s...@googlegroups.com

$A(['foo','bar','baz']).each(function(elm,idx){
if(elm == 'bar') alert(idx);
});

Walter


kstubs

unread,
Aug 24, 2011, 1:47:34 AM8/24/11
to prototype-s...@googlegroups.com
OK, that is exactly what I wrote.  I'm always looking for that really snazzy shortcut.

Thanks.

Victor

unread,
Aug 24, 2011, 3:55:41 AM8/24/11
to prototype-s...@googlegroups.com
For Enumeration (Hash, ObjectRange etc.) use Enumeration#each

var index = -1;
enumeration.each(function(e, i) {
  if (e === 'bar') {
    index = i;
    // looking for the first occurrence
    throw $break;
  }
});

for Array you can use shorter

var index = ['foo','bar','baz'].indexOf('bar');


Reply all
Reply to author
Forward
0 new messages