Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .each() without index [duplicate]

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

like image 354
Nano Avatar asked Jun 06 '14 20:06

Nano


People also ask

How to avoid jQuery each method?

So I present you with the 7 ways to avoid jQuery Each method with an equivalent JavaScript .forEach () method on your website. The 1 st parameter is a ‘Required’ one and is a callback function that runs for each element of the array. This callback function has 3 parameters - function (currentValue, index, arr)

What is the use of each in jQuery?

What is jQuery.each () jQuery’s each () function is used to loop through each element of the target jQuery object — an object that contains one or more DOM elements, and exposes all jQuery functions. It’s very useful for multi-element DOM manipulation, as well as iterating over arbitrary arrays and object properties.

How to execute each matched element in a list using jQuery?

.each ( function (index, Element) ) function (index, Element)A function to execute for each matched element. ...where index will be the index and element will be the option element in list surprise to see that no have given this syntax. jQuery.each ( jQuery ('#list option'), function (indexInArray, valueOfElement) { //your code here });

How do you pass an index to a jQuery callback?

The index of the current element within the collection is passed as an argument to the callback. The value (the DOM element in this case) is also passed, but the callback is fired within the context of the current matched element so the this keyword points to the current element as expected in other jQuery callbacks.


1 Answers

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
like image 192
meagar Avatar answered Sep 21 '22 09:09

meagar