Take the 2-minute tour ×

The Problem

I need some properties of each item that query returns as array of objects

What we do right now

$('selector').each(create array of texts or whatsoever);

What is wrong?

Why should we iterate over elements for each requirement?

Here is the same thing in LINQ

books.Select(b=> b.Text).ToArray();
// or another sample
books.Select(b=> new { b.Text , b.ISBN}).ToArray();

What do we/i need?

Selecting specified element properties as array of objects without reiterating over elements - something that called deferred enumeration

Is it possible?

I think yes it is, because right now i have some ideas.

I already know about 'map' and 'each' methods, beware that both of them will reiterate over selector items.So yes map will results what i want, but not exactly. Also note that even C# that have more better performance than java-script is using this method that i'm asking for, So this is a requirement for all web-developers not just me and that's exactly why i'm sharing the problem here to be solved.

Research result

Iteration over elements using jQuery is inevitable because jQuery itself is not finding elements using loop over document elements until that's necessary for example pseudo classes, so for implementing deferred enumeration over jQuery infrastructure we have to force jQuery to iterate over items, and that makes it slow.

I'd implemented deferred enumeration using a pseudo class named 'select' and passing in .net like lambda expression.That was handling selects very nicely just like .net, but result was slower than map and each jQuery methods up to 50%.Finally the answer is jQuery map method.

share|improve this question
Are you asking if it is possible without iteration in jQuery? Or are you telling us that it is possible? –  user113716 Jun 26 '11 at 21:44
add comment

1 Answer

up vote 5 down vote accepted

You might be looking for the map() method:

var books_text = $(".book").map(function() {
    return $(this).text();

var books_info = $(".book").map(function() {
    var $this = $(this);
    return {
        text: $this.text(),
        isbn: $this.attr("isbn")  // If stored in an attribute.
share|improve this answer
thanks for fast response, but map is reiterating over elements again, and not just on elements. it is two for loop inside each other , one for elements and one for each key on object. –  Beygi Jun 26 '11 at 21:35
@Beygi, I'm afraid I don't understand, do you mean map() iterates twice over the elements? Also, by each key on object, do you mean each attribute of the element? –  Frédéric Hamidi Jun 26 '11 at 21:36
No, I'm not, if you look at line 756 of jquery 1.6.1 you can see that it's iterating over each key of elements, and because we have more than one elements the result would be..., and also it has been commented : Go through every key on the object –  Beygi Jun 26 '11 at 21:40
And also remember we want to do this without another loop, because if we do $.each will do the same. –  Beygi Jun 26 '11 at 21:43
Perhaps Beygi is looking for a deferred enumeration mechanism? If we use javascript to map an array into a new array with the goal to do something with the elements in the new array, we would have to iterate over the elements twice, once for mapping and once for doing that something. Having a deferred enumeration mechanism we would only have to iterate once - the mapping function would be invoked on each element as they are visited. This is easily accomplished in C#. Don't know the best way to do it in javascript though... –  Mårten Wikström Jun 26 '11 at 22:24
show 17 more comments

Your Answer


