Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using $(array).each to iterate over array

Tags:

arrays

jquery

I recently came upon a jQuery construction I hadn't seen before. Here's a simplified version:

$([ '1', '2', '3' ]).each( function( index, value ) {
  console.log( "index", index, "value", value );
});

This iterates over all the elements of the array [ '1', '2', '3' ]. The thing is, I'm used to seeing $(...) used with a CSS selector, but I haven't seen it used on a newly declared array, as is being done here. It seems to work, though (tested with Firefox 34 and Chromium 39).

Q1: Am I correct in understanding that this is equivalent to

var a = [ '1', '2', '3' ];
for ( var i = 0, value; value = a[i]; i++ ) {
  console.log( "index", i, "value", value );
}

If not, what are the differences? (apart from the fact that this declares variables a, i and value).

Q2: As far as iterating over arrays in jQuery is concerned, I'm more used to $.each (not to be confused with $(selector).each as used above). Would the above be equivalent to

$.each( [ '1', '2', '3' ], function( index, value ){
  console.log( "index", index, "value", value );
});

if yes, why are there two such extremely similar constructions in jQuery? Between the two, what is the preferred way to iterate over arrays, or is this just a matter of personal style?

like image 511
Malte Skoruppa Avatar asked Jan 05 '15 16:01

Malte Skoruppa


People also ask

How do you iterate through an array in jQuery?

Answer: Use the jQuery. each() function each() or $. each() can be used to seamlessly iterate over any collection, whether it is an object or an array. However, since the $. each() function internally retrieves and uses the length property of the passed array or object.

How do you iterate through an array?

Iterating over an array You can iterate over an array using for loop or forEach loop. Using the for loop − Instead on printing element by element, you can iterate the index using for loop starting from 0 to length of the array (ArrayName. length) and access elements at each index.

Can you iterate array by using forEach loop How?

forEach() method iterates over the array items, in ascending order, without mutating the array. The first argument of forEach() is the callback function called for every item in the array. The second argument (optional) is the value of this set in the callback.

What does .each do in JS?

The $. each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.


1 Answers

Q1. Yes.

Q2. jQuery accepts arrays (and array-like objects) and turns them into jQuery objects.

You can see that easily by issuing, on the browser console:

console.dir($([ '1', '2', '3' ]))
> VM199:2 e.fn.e.init[3]

That's a jQuery object that call returns. They can be iterated over with .each(). This facility is meant to enable you to do this (contrived example):

$(document.getElementsByTagName("A")).each(func);

Doing so with a plain array of strings works, and will likely continue to work in the future, however I still see that as a mis-use of the API and would recommend the proper approach:

$.each(['1', '2', '3' ], func);
like image 190
Tomalak Avatar answered Oct 01 '22 07:10

Tomalak