Questions tagged [each]

An iterator function or language struct which can be used to iterate over arrays or lists.

A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.

jQuery.each( array, callback )
// or
jQuery.each( object, callback )
3858 questions
3553
votes
48 answers

How do I loop through or enumerate a JavaScript object?

I have a JavaScript object like the following: var p = { "p1": "value1", "p2": "value2", "p3": "value3" }; How do I loop through all of p's elements (p1, p2, p3...) and get their keys and values?
Tanmoy
  • 44,392
  • 16
  • 45
  • 55
262
votes
4 answers

How to use continue in jQuery each() loop?

In my application i am using AJAX call. I want to use break and continue in this jQuery loop. $('.submit').filter(':checked').each(function() { });
Ravi Kant
  • 4,785
  • 2
  • 24
  • 23
227
votes
9 answers

"for" vs "each" in Ruby

I just had a quick question regarding loops in Ruby. Is there a difference between these two ways of iterating through a collection? # way 1 @collection.each do |item| # do whatever end # way 2 for item in @collection # do whatever end Just…
mportiz08
  • 10,206
  • 12
  • 40
  • 42
225
votes
4 answers

Access a variable outside the scope of a Handlebars.js each loop

I have a handlebars.js template, just like this: {{externalValue}} And this is the generated…
lucke84
  • 4,516
  • 3
  • 37
  • 58
201
votes
12 answers

Invoking a jQuery function after .each() has completed

In jQuery, is it possible to invoke a callback or trigger an event after an invocation of .each() (or any other type of iterative callback) has completed. For example, I would like this "fade and remove" to…
Luther Baker
  • 7,236
  • 13
  • 46
  • 64
138
votes
6 answers

How to iterate over array of objects in Handlebars?

This might seem a silly question but I can't seem to find the answer anywhere. I'm hitting this Web API that returns an array of objects in JSON format: Handlebars docs shows the following example:
    {{#each people}} …
empz
  • 11,509
  • 16
  • 65
  • 106
122
votes
9 answers

What's the safest way to iterate through the keys of a Perl hash?

If I have a Perl hash with a bunch of (key, value) pairs, what is the preferred method of iterating through all the keys? I have heard that using each may in some way have unintended side effects. So, is that true, and is one of the two following…
Rudd Zwolinski
  • 26,712
  • 17
  • 57
  • 60
114
votes
5 answers

jQuery .each() index?

I am using $('#list option').each(function(){ //do stuff }); to loop over the options in a list. I am wondering how I could get the index of the current loop? as I dont want to have to have var i = 0; and inside the loop have i++;
Hailwood
  • 89,623
  • 107
  • 270
  • 423
109
votes
12 answers

How can I update code that uses the deprecated each() function?

With PHP 7.2, each is deprecated. The documentation says: Warning This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged. How can I update my code to avoid using it? Here are some examples: $ar =…
yokogeri
  • 1,217
  • 2
  • 8
  • 11
104
votes
9 answers

Tell the end of a .each loop in ruby

If i have a loop such as users.each do |u| #some code end Where users is a hash of multiple users. What's the easiest conditional logic to see if you are on the last user in the users hash and only want to execute specific code for that last…
Splashlin
  • 7,225
  • 12
  • 46
  • 50
102
votes
3 answers

jQuery each loop in table row

I am having something like:
ANP
  • 15,287
  • 22
  • 58
  • 79
93
votes
4 answers

C++ for each, pulling from vector elements

I am trying to do a foreach on a vector of attacks, each attack has a unique ID say, 1-3. The class method takes the keyboard input of 1-3. I am trying to use a foreach to run through my elements in m_attack to see if the number matches, if it…
Springfox
  • 1,359
  • 2
  • 11
  • 11
90
votes
8 answers

How to add pause between each iteration of jQuery .each()?

I'm grabbing an array of jQuery objects and then via .each() modifying each individual jquery with in the array. In this case I'm updated the class names to trigger a -webkit-transition-property to utilize a css transition. I'd like there to be a…
DA.
  • 39,848
  • 49
  • 150
  • 213
85
votes
8 answers

What is the difference between $.each(selector) and $(selector).each()

What is the difference between this: $.each($('#myTable input[name="deleteItem[]"]:checked').do_something()); and this: $('#myTable input[name="deleteItem[]"]:checked').each(function() { do_something }); The html for the table cell that is being…
marky
  • 4,878
  • 17
  • 59
  • 103
85
votes
3 answers

@each loop with index

I was wondering if you can get an element index for the @each loop. I have the following code, but I was wondering if the $i variable was the best way to do this. Current code: $i: 0; $refcolors: #55A46A, #9BD385, #D9EA79, #E4EE77, #F2E975, #F2D368,…
ignacioricci
  • 1,236
  • 1
  • 8
  • 9
1
2 3
99 100