7

I am wondering if there is a way to use the jQuery $.each() function without getting the index of the current element.

I am using the $.each() function verry often, but i always have to declare unused key variables like this:

$.each(data,function(unusedKey,subData){
    //Do something with subData
});

Is there another jQuery function which just returns the values?

Note:

I want to use a jQuery Function! I know that i could use a simple for(var key in data), but I realy want to use jQuery!

By the way:

is the $.each() function noteable slower? Or can I use it at the most unnecessary points?

Edit:

By data i mean JSON-Data

Nano
  • 1,398
  • 6
  • 20
  • 32
  • You don't have to supply any function arguments whatsoever to `.each()`, just do `$('.example').each(function() { // blah blah });` – Ennui Jun 06 '14 at 20:09
  • 2
    So, you're complaining about having to define the first argument to the callback just so you can use the 2nd argument. That's a bit ridiculous. The arguments are always there whether you declare a name for them or not (you can reach the one you want with `arguments[1]`) so it makes no difference if you make up a name for the first argument. I see no reason why this question matters in any way. – jfriend00 Jun 06 '14 at 20:10
  • 1
    jQuery's each is dumb and backwards compared to all the other now-standard array iteration methods. native is faster and has better arity (imho): [].forEach.call(data,function(subData){ }); or $(whatev).toArray().map(function(val,index){...}); the native methods also allow you to use _this_ instead of wasting it on repeating the current value, which makes iteration functions far more re-usable and generic. – dandavis Jun 06 '14 at 20:14
  • http://stackoverflow.com/questions/8356227/skipping-optional-function-parameters-in-javascript/8356945 – adeneo Jun 06 '14 at 20:18
  • @adeneo I asked specifically for the jQuery `$.each()` function...your duplicate link doesnt answer my question...by the way, i already got an answer... – Nano Jun 06 '14 at 20:20

1 Answers1

17

Just ignore the arguments altogether, and use this.

$('div').each(function () {
  $(this).hide();
});

or

$.each([1, 2, 3], function () {
  console.log(this * 2) 
});

// outputs 2, 4, 6
user229044
  • 232,980
  • 40
  • 330
  • 338
  • by data i meant json-Data :S sry i shoud mention this – Nano Jun 06 '14 at 20:09
  • Thanks! I didnt know i could use `this`! – Nano Jun 06 '14 at 20:12
  • There is no such thing as "json-Data". It's just data after it's decoded from JSON, and it's identical to an array/hash created by any other means. – user229044 Jun 06 '14 at 20:12
  • I thought i could use `this` in combination with `$.each()` just if the data is an jQuery Element/Object :S – Nano Jun 06 '14 at 20:13
  • 2
    *"The $.each() function is not the same as $(selector).each(), which is used to iterate, exclusively, over a jQuery object."* [jQuery.each()](http://api.jquery.com/jquery.each/). `$([]).each` work, but shouldn't be used for normal arrays... – Karl-André Gagnon Jun 06 '14 at 20:18