Bug Tracker

Modify

Ticket #1886 (closed enhancement: plugin)

Opened 6 years ago

Last modified 3 months ago

Add jQuery.reduce()

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

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

Change History

comment:1 Changed 6 years ago by mw44118

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

comment:2 Changed 6 years ago by flesler

  • Status changed from new to closed
  • Resolution set to wontfix

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

comment:3 Changed 6 years ago by flesler

  • Milestone set to 1.2.4

comment:4 Changed 3 years ago by rwaldron

#8379 is a duplicate of this ticket.

comment:5 follow-up: ↓ 6 Changed 3 years ago by tokland@…

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

comment:6 in reply to: ↑ 5 Changed 2 years ago by anonymous

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

comment:7 Changed 21 months ago by anonymous

Hi. This would be nice to have. Thanks!

comment:8 Changed 21 months ago by rwaldron

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

comment:9 Changed 21 months ago by anonymous

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?

comment:10 Changed 21 months ago by rwaldron

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

comment:11 Changed 21 months ago by anonymous

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.

comment:12 Changed 21 months ago by dmethvin

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

Because we need and use them internally.

comment:13 Changed 19 months ago by xixixao

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!

comment:14 Changed 19 months ago by rwaldron

#11700 is a duplicate of this ticket.

comment:15 Changed 17 months ago by lukas.eder@…

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

comment:16 Changed 17 months ago by jquery@…

Just Do It :)

comment:17 Changed 17 months ago by brettz9@…

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');
});

comment:18 Changed 17 months ago by dmethvin

  • Status changed from closed to reopened
  • Resolution wontfix deleted
  • Milestone 1.2.4 deleted

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

comment:19 Changed 17 months ago by dmethvin

  • Status changed from reopened to closed
  • Resolution set to plugin

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

comment:20 Changed 14 months ago by anonymous

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.

comment:21 Changed 14 months ago by scott.gonzalez

@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.

comment:22 Changed 11 months ago by anonymous

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 ?

comment:23 follow-up: ↓ 24 Changed 11 months ago by 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.

comment:24 in reply to: ↑ 23 Changed 11 months ago by rwaldron

Replying to 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.

comment:25 follow-up: ↓ 27 Changed 9 months ago by pedro.ladaria@…

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.

comment:26 Changed 7 months ago by lutzissler

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

comment:27 in reply to: ↑ 25 Changed 7 months ago by Martin Geisler <martin@…>

Replying to 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.

comment:28 follow-up: ↓ 30 Changed 5 months ago by 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);

comment:29 Changed 3 months ago by cbarrick1@…

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

comment:30 in reply to: ↑ 28 Changed 3 months ago by m_gol

Replying to 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

Last edited 3 months ago by m_gol (previous) (diff)

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.