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
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 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.
.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 });
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With