Select range of array items

19 views
Skip to first unread message

kstubs

unread,
Jan 12, 2012, 1:04:04 PM1/12/12
to prototype-s...@googlegroups.com
I have an array of objects.  I'd like to grab items from the array by specifying a range like 1-10, or 11-20, or 5-7, and so on..
Seems trivial. How is this done with Prototype?

Walter Lee Davis

unread,
Jan 12, 2012, 1:38:57 PM1/12/12
to prototype-s...@googlegroups.com

On Jan 12, 2012, at 1:04 PM, kstubs wrote:

> I have an array of objects. I'd like to grab items from the array by specifying a range like 1-10, or 11-20, or 5-7, and so on..
> Seems trivial. How is this done with Prototype?
>

var arr = $w('the quick brown fox jumped over the lazy dog');
$R(1,5).each(function(n){
console.log(arr[n]);
});

//-> should output the words quick through over, one to a line, in your console.

I'm sure there's another way, but that's what I would do.

Walter


> --
> You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/prototype-scriptaculous/-/hyCKraRuIykJ.
> To post to this group, send email to prototype-s...@googlegroups.com.
> To unsubscribe from this group, send email to prototype-scripta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.

kstubs

unread,
Jan 12, 2012, 1:54:23 PM1/12/12
to prototype-s...@googlegroups.com
That should work.  Was imagining a method off the array object directly.
Thanks.

Karl..

Walter Lee Davis

unread,
Jan 12, 2012, 1:59:42 PM1/12/12
to prototype-s...@googlegroups.com
Well, you can always use the generic Array.slice() method.

$A($w('the quick brown fox jumped over the lazy dog').slice(1,5)).each(function(elm){console.log(elm);});

Walter

> --
> You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.

> To view this discussion on the web visit https://groups.google.com/d/msg/prototype-scriptaculous/-/e0tUAEIkO_4J.

kstubs

unread,
Jan 12, 2012, 2:10:24 PM1/12/12
to prototype-s...@googlegroups.com
Ahh yes, of course!  
Thanks Walter.

Karl..

Walter Lee Davis

unread,
Jan 12, 2012, 2:14:48 PM1/12/12
to prototype-s...@googlegroups.com
Just to point out, the $A( ) wrapper around the slice product was there because you can't guarantee an "extended" array in IE without it. Every other browser would be fine. Also, if you weren't using any Prototype magic on the output array, you could skip that as well.

Walter

> --
> You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.

> To view this discussion on the web visit https://groups.google.com/d/msg/prototype-scriptaculous/-/zOftjBzBA9EJ.

kstubs

unread,
Jan 12, 2012, 4:56:16 PM1/12/12
to prototype-s...@googlegroups.com
Well, I think I need that magic since I'm expecting to enumerate over the object after I've made my selection.
Thanks for the follow up.
Karl..

T.J. Crowder

unread,
Jan 12, 2012, 6:06:28 PM1/12/12
to Prototype & script.aculo.us
On Jan 12, 7:14 pm, Walter Lee Davis <wa...@wdstudio.com> wrote:
> Just to point out, the $A( ) wrapper around the slice product was
> there because you can't guarantee an "extended" array in IE without it.
> Every other browser would be fine.

No, IE allows Prototype to automagically extend arrays just fine. It's
only the DOM element extensions that are not automatic in IE. This
works in IE6, for instance:
http://jsbin.com/afocoy

All that `$A()` does in that code is needlessly duplicate the array.
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

kstubs

unread,
Jan 12, 2012, 10:28:30 PM1/12/12
to prototype-s...@googlegroups.com
Thanks for the followup TJ.
Karl..

Victor

unread,
Jan 13, 2012, 1:34:19 AM1/13/12
to prototype-s...@googlegroups.com

automagically

:)
Reply all
Reply to author
Forward
0 new messages