Skip to main content

Bug Tracker

Side navigation

#1886 closed enhancement (plugin)

Opened November 02, 2007 06:12PM UTC

Closed December 04, 2012 03:50PM UTC

Last modified January 20, 2014 05:30PM UTC

Add jQuery.reduce()

Reported by: genezys Owned by:
Priority: trivial Milestone:
Component: core Version: 1.2.1
Keywords: Cc:
Blocked by: Blocking:
Description

As jQuery already provides a Map algorithm for arrays, it would be nice to have a Reduce algorithm too. These too are the fundamentals of functionnal programming and jQuery already has a very good Map algorithm

I already did a Reduce implementation using jQuery, here it is:

jQuery.reduce = function(arr, valueInitial, fnReduce)
{
	jQuery.each( arr, function(i, value)
	{
		valueInitial = fnReduce.apply(value, [valueInitial, i, value]);
	});
	return valueInitial;
}

This is not really long and can really enhance the meaning of some code.

I think it can also help clarify some jQuery internal code that concatenate to a single value like text() or html().

Thanks

Attachments (0)
Change History (30)

Changed March 19, 2008 06:51PM UTC by mw44118 comment:1

Yeah, I really want this. It would remove the need for a lot of silly for-loops in my code.

Changed May 11, 2008 11:43PM UTC by flesler comment:2

resolution: → wontfix
status: newclosed

This won't be useful in the core, it can always be included in a plugin.

Reopen if you find places where it can reduce code size in the code, and please point them out.

Thanks

Changed May 13, 2008 05:31PM UTC by flesler comment:3

milestone: → 1.2.4

Changed February 25, 2011 08:12PM UTC by rwaldron comment:4

#8379 is a duplicate of this ticket.

Changed May 25, 2011 08:29AM UTC by tokland@gmail.com comment:5

Not that I want to revive a dead horse, but given that ''Array.reduce'' found its way into ECMA 1.8 I guess it wouldn't be too bold a move to include it also in Jquery with the same syntax:

https://developer.mozilla.org/en/New_in_JavaScript_1.8

https://bugzilla.mozilla.org/show_bug.cgi?id=363040

Changed March 27, 2012 01:14AM UTC by anonymous comment:6

I just came here in search of this. It would be nice to have.

Changed July 23, 2012 07:44PM UTC by anonymous comment:7

Hi. This would be nice to have. Thanks!

Changed July 23, 2012 08:24PM UTC by rwaldron comment:8

reduce is available in native, ES5 compliant JavaScript implementations as well as many open-source utility libraries, see also: http://lodash.com/docs#reduce

Changed August 09, 2012 03:11PM UTC by anonymous comment:9

reduce is not available in IE8: http://kangax.github.com/es5-compat-table/#.

IE8 represents significant marketshare (~26%) and is probably exclusive in many Windows domain intranets. Why not include it?

Changed August 09, 2012 03:15PM UTC by rwaldron comment:10

There are plenty of libraries that specialize in utility functions, I recommend Lodash http://lodash.com/

Changed August 09, 2012 10:20PM UTC by anonymous comment:11

Why are $.each and $.map in the core? Plugins/shims can be used for those, too. The point is that they are useful and it's nice to be able to rely on a common set of features everywhere that jQuery is available.

Sometimes adding plugins into a project isn't our call, or requires buy-in from a senior dev.

I'm mostly curious how $.each and $.map are ok, while $.reduce is completely orthogonal to jQuery's core values.

Changed August 09, 2012 10:21PM UTC by dmethvin comment:12

Why are $.each and $.map in the core?

Because we need and use them internally.

Changed September 23, 2012 11:50AM UTC by xixixao comment:13

Suprised jQuery doesn't have this, I think it is now widely used as a standard language extension and as such it should have this basic function!

Changed September 25, 2012 02:46PM UTC by rwaldron comment:14

#11700 is a duplicate of this ticket.

Changed November 20, 2012 12:38PM UTC by lukas.eder@gmail.com comment:15

Even Java is going to add .reduce() now to the JDK 8 Collections API:

http://cr.openjdk.java.net/~briangoetz/lambda/sotc3.html

I'd really love to see this in the jQuery core

Changed November 26, 2012 03:11AM UTC by jquery@therealcha0s.net comment:16

Just Do It :)

Changed December 04, 2012 07:51AM UTC by brettz9@yahoo.com comment:17

Yeah, I hate to add the "me too", but this both jives for parity with other array-like methods, and in addition to use wth arrays, would be great for custom ways to serialize nodes, if present also on elements.

var hrefs = $('a').reduce('Your links: ', function (prev, curr) {
    return prev + '<br>' + $(curr).attr('href');
});

Changed December 04, 2012 03:49PM UTC by dmethvin comment:18

milestone: 1.2.4
resolution: wontfix
status: closedreopened

This can be done as a plugin. If you have created one, please post a link to it here.

Changed December 04, 2012 03:50PM UTC by dmethvin comment:19

resolution: → plugin
status: reopenedclosed

We wouldn't land this in jQuery unless we needed it internally.

Changed March 01, 2013 10:25AM UTC by anonymous comment:20

So are we to interpret all $.xxx functions as internal only to be used by jQuery itself? Honestly. The line of argument for keeping this out doesn't work. map/reduce goes together.

Changed March 01, 2013 12:50PM UTC by scottgonzalez comment:21

@anonymous If the method is documented, it's not for internal use only. The functionality is prescribed by the needs of the core library. The argument for keeping this out is perfectly valid. If the method isn't useful inside jQuery, there is absolutely no reason to add it. jQuery is a DOM library, if reducing arrays doesn't help jQuery do it's job, then it doesn't belong.

Changed June 06, 2013 04:43AM UTC by anonymous comment:22

I think we should have the "reduce" functionality included. This also bridges the gap between the old browsers and new browsers which have "reduce" function in built.I know there are libraries out there like underscore.js which has this functionality, but to use "underscore.js" just to have "reduce" functionality is a crazy idea :) After all we have all these awesome libraries to do better with less code, isn't ?

Changed June 11, 2013 09:36PM UTC by anonymous comment:23

Given an implementation of "map", there ought to be a "reduce". Maybe it was an error to introduce "map" in the first place, as it creates an expectation that there is also a "reduce". But it is what it is, so add "reduce" and be done with it.

Changed June 11, 2013 09:53PM UTC by rwaldron comment:24

Replying to [comment:23 anonymous]:

Given an implementation of "map", there ought to be a "reduce". Maybe it was an error to introduce "map" in the first place, as it creates an expectation that there is also a "reduce". But it is what it is, so add "reduce" and be done with it.

No.

Changed July 28, 2013 10:21AM UTC by pedro.ladaria@gmail.com comment:25

The argument to not include "reduce" is invalid.

Just an example:

jQuery has "unique" (remove duplicates from an array) and works this way:

1) copies the array

2) sorts it

3) walks the whole array to find duplicates and copies into a new array the results.

Array unique using EC5 code (shorter and faster):

var arrayUnique = function(a) {
  return Array.isArray(a) ? a.reduce(function(p, c) {
    if (p.indexOf(c) < 0) p.push(c);
    return p;
  }, []) : undefined;
};

reduce function can be used to improve lots of parts of the code.

Changed October 04, 2013 08:17AM UTC by lutzissler comment:26

I have created a gist that might be useful for anybody looking for a reduce implementation: https://gist.github.com/lutzissler/6822599

Changed October 10, 2013 01:27PM UTC by Martin Geisler <martin@geisler.net> comment:27

Replying to [comment:25 pedro.ladaria@…]:

jQuery has "unique" (remove duplicates from an array) and works this way: 1) copies the array 2) sorts it 3) walks the whole array to find duplicates and copies into a new array the results. Array unique using EC5 code (shorter and faster):
> var arrayUnique = function(a) {
>   return Array.isArray(a) ? a.reduce(function(p, c) {
>     if (p.indexOf(c) < 0) p.push(c);
>     return p;
>   }, []) : undefined;
> };
> 

This is a bad example. Your function has O(n**2) (quadratic) complexity vs O(n log n) for the algorithm you describe for jQuery. This means that it will scale badly and it's a bad place to use a reduce function.

Changed November 13, 2013 10:12AM UTC by fabio.montefuscolo@gmail.com comment:28

A alternative to have reduce is reuse it from Array. Something like this:

  (function($){

    $.fn.reduce = function(callback, initial){
      return Array.prototype.reduce.call(this, callback, initial);
    }

  })(jQuery);

Changed January 20, 2014 05:27PM UTC by cbarrick1@gmail.com comment:29

As mentioned before, Array.prototype.reduce exists in ECMAScript 5.1

Rather than:

$(...)
  .map(...)
  .reduce(...)

In most modern browsers you can already do:

$(...)
  .map(...)
  .get()
  .reduce(...)

For a compatibility table and pollyfill see MDN

Changed January 20, 2014 05:30PM UTC by m_gol comment:30

_comment0: Replying to [comment:28 fabio.montefuscolo@…]: \ > A alternative to have reduce is reuse it from Array. Something like this: \ > \ > {{{ \ > (function($){ \ > \ > $.fn.reduce = function(callback, initial){ \ > return Array.prototype.reduce.call(this, callback, initial); \ > } \ > \ > })(jQuery); \ > }}} \ \ Or just: \ \ $.fn.reduce = [].reduce \ \ A little smaller. :P1390239043039665
_comment1: Replying to [comment:28 fabio.montefuscolo@…]: \ > A alternative to have reduce is reuse it from Array. Something like this: \ > \ > {{{ \ > (function($){ \ > \ > $.fn.reduce = function(callback, initial){ \ > return Array.prototype.reduce.call(this, callback, initial); \ > } \ > \ > })(jQuery); \ > }}} \ \ Or just: \ {{{ \ $.fn.reduce = [].reduce \ }}} \ A little smaller. :P1390239055860535

Replying to [comment:28 fabio.montefuscolo@…]:

A alternative to have reduce is reuse it from Array. Something like this:
>   (function($){
> 
>     $.fn.reduce = function(callback, initial){
>       return Array.prototype.reduce.call(this, callback, initial);
>     }
> 
>   })(jQuery);
> 

Or just:

$.fn.reduce = [].reduce;

A little smaller. :P