Underline each method that returns values

This is a stripped down example of what I'm trying to do. I am trying to get my wrapper function myElements to return the elements coming from the underline of each iterator. I can trace the els values ​​inside the _.each function, but how can I make my function return these values ​​when called from my buttonClickListener ?

 buttonClickListenr: function(event){ if ( $(event.target).is( myElements() )) return; ..// otherwise move on in my event phase } myElements: function(){ var filter=['a','b']; _.each(filter, function(el){ return el }); } 
+4
source share
2 answers

What you are trying to do does not really make sense. The include statement is probably more useful for you:

if ( _(filter).include($(event.target)) ){ return; }

0
source

The each function will call a callback once for each element and not group the return values ​​as you expect. Instead, you need to save them in a structure that remains after each method completes.

Try to execute

 getNums: function(){ var filter=['20','2']; var result = []; _.each(filter, function(num){ result.push(num); }); return result; } 

EDIT

The OP found out that they just want to see if event.target in the array. In this case, you just want to use the indexOf method. for instance

 getNums: function() { return ['20', '2']; }, buttonClickListener: function(event){ if (this.getNums().indexOf(event.target) >= 0) { // It present } } 
+1
source

All Articles