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 5 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 5 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 5 years ago by flesler

  • Milestone set to 1.2.4

comment:4 Changed 2 years ago by rwaldron

#8379 is a duplicate of this ticket.

comment:5 follow-up: ↓ 6 Changed 2 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 14 months ago by anonymous

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

comment:7 Changed 10 months ago by anonymous

Hi. This would be nice to have. Thanks!

comment:8 Changed 10 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 9 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 9 months ago by rwaldron

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

comment:11 Changed 9 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 9 months ago by dmethvin

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

Because we need and use them internally.

comment:13 Changed 8 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 8 months ago by rwaldron

#11700 is a duplicate of this ticket.

comment:15 Changed 6 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 6 months ago by jquery@…

Just Do It :)

comment:17 Changed 6 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 6 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 6 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 3 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 3 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.

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.