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
25
votes
6 answers

jQuery loop through data() object

Is it possible to loop through a data() object? Suppose this is my code: $('#mydiv').data('bar','lorem'); $('#mydiv').data('foo','ipsum'); $('#mydiv').data('cam','dolores'); How do I loop through this? Can each() be used for this?
bart
  • 14,958
  • 21
  • 75
  • 105
25
votes
1 answer

What is the difference between Groovy each and forEach?

Simple question that I haven't been able to find a simple answer for on the googles: what is the difference between Groovy's each and forEach loops? I made a simple example and the syntax and behavior seem identical: [1, 2].each { println it } …
orbfish
  • 7,381
  • 14
  • 58
  • 75
25
votes
4 answers

for loop in thymeleaf

How can I do the following (java): for(int i = 0; i < 81 ; i+=20){ //Should loop through 5 times! } in Thymeleaf? I've tried this:
BILL
  • 4,711
  • 10
  • 57
  • 96
24
votes
3 answers

Looping through input fields for validation using Jquery each()

I'm making a form and would like the code to execute only if the input values are numbers. I'm trying to avoid using some kind of validation plugin and was wondering if there is a way to loop through the input fields and check for the values. I've…
Alex Berg
  • 747
  • 3
  • 8
  • 17
24
votes
4 answers

Why does Array#each return an array with the same elements?

I'm learning the details of how each works in ruby, and I tried out the following line of code: p [1,2,3,4,5].each { |element| el } And the result is an array of [1,2,3,4,5] Why is the return value of each the same array? Doesn't each just provide…
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
23
votes
5 answers

Further explanation of Svelte's keyed each block

I don't understand this section in the tutorial: https://svelte.dev/tutorial/keyed-each-blocks. I can see the things array is updated correctly so the right thing.color is passed as expected. But by the first sentence "By default, when you modify…
Ziyuan
  • 4,215
  • 6
  • 48
  • 77
23
votes
2 answers

DRYest way to check if in the first iteration of an .each loop in Ruby/Rails

In my .erb, I have a simple each loop: <%= @user.settings.each do |s| %> .. <% end %> What is the simplest way to check if it's currently going through its first iteration? I know I could set i=0...i++ but that is just too messy inside of an…
Wes Foster
  • 8,770
  • 5
  • 42
  • 62
22
votes
3 answers

javascript find if value is NOT IN array

My problem with this is that the loop keeps going into the if statement even for duplicate barcodes. I'm trying to enter the if statement only for unique barcodes but at the end of the loop myArray has duplicates in it....why? var myArray = new…
sadmicrowave
  • 39,964
  • 34
  • 108
  • 180
22
votes
4 answers

Excel VBA For Each Worksheet Loop

I am working on code to basically go through each sheet in my Workbook, and then update column widths. Below is the code I wrote; I don't receive any errors, but it also doesn't actually do anything. Any help is greatly appreciated! Option…
Dakota
  • 464
  • 3
  • 7
  • 19
22
votes
4 answers

jQuery - Looping through elements with specific Attribute

I know how to loop through the inputs below, searching for the ones with a specific class of "testers" And here's how I do that:
coffeemonitor
  • 12,780
  • 34
  • 99
  • 149
21
votes
2 answers

jQuery: Looping through object properly?

I am trying to loop through the below shown JS object with the following code snippet, while needing to fetch both the index key as well as the inner object. How on earth should I do this, as the following doesn't work? The object: ({ prop_1:["1",…
Industrial
  • 41,400
  • 69
  • 194
  • 289
21
votes
3 answers

jQuery.each implementation differs from native Array.forEach

Does anyone why is the otherwise excellent jQuery.each function is designed differently from the (now) native Array.forEach? F.ex: var arr = ['abc','def']; arr.forEach(function(entry, index) { console.log(entry); // abc / def }); This makes…
David Hellsing
  • 106,495
  • 44
  • 176
  • 212