[ 4 posts ]

mingfai

  • Joined: Mon Dec 15, 2008 6:58 pm
  • Posts: 22
  • Offline
  • Profile

is there a way to get the index in Y.each function?

Post Posted: Mon Mar 09, 2009 8:19 am
+0-
hi,

first of all, i found the use of NodeList.each very cool.
Y.all('#test li').each( function(li){
Y.log('li: ' + li.get('innerHTML')
} )

thanks for the great lib.

When using each, is there a way to get the index? should the each function pass one more argument as the index?

Eric Ferraiuolo

YUI Developer

  • Username: ericf
  • Joined: Mon Jan 12, 2009 8:26 pm
  • Posts: 380
  • Location: Boston, MA
  • Twitter: ericf
  • GitHub: ericf
  • Gists: ericf
  • IRC: eric_f
  • YUI Developer
  • Offline
  • Profile

Re: is there a way to get the index in Y.each function?

Post Posted: Mon Mar 09, 2009 8:38 am
+0-
I was running into this myself... ended up using Y.Array.indexOf to get the index. This didn't seem efficient to me.

I found myself resorting to for-looping when I was in need of the index; seems the each method is better for delegation to another method and where the method that's getting delegated to doesn't need to know about the index.
Eric Ferraiuolo

mingfai

  • Joined: Mon Dec 15, 2008 6:58 pm
  • Posts: 22
  • Offline
  • Profile

Re: is there a way to get the index in Y.each function?

Post Posted: Mon Mar 09, 2009 8:57 am
+0-
that works only for Array, but if the data is an object or a NodeList, it won't work
Code:
  var data = [{one:1}, {two:2}]
      Y.each(data, function(v,k,a){
        Y.log('v: ' + v + ', k: ' + k + ', a: ' + a + ', index: ' + Y.Array.indexOf(data, v))
      })


replace with:
Code:
var data = {one:1,two:2}


that's the problem!

Eric Ferraiuolo

YUI Developer

  • Username: ericf
  • Joined: Mon Jan 12, 2009 8:26 pm
  • Posts: 380
  • Location: Boston, MA
  • Twitter: ericf
  • GitHub: ericf
  • Gists: ericf
  • IRC: eric_f
  • YUI Developer
  • Offline
  • Profile
Tags:

Re: is there a way to get the index in Y.each function?

Post Posted: Mon Mar 09, 2009 9:33 am
+0-

Do This

Click here to see the revision history on this Gist.
Eric Ferraiuolo
  [ 4 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum