Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a jQuery map utility that doesn't automically flatten?

I'm mapping an array of two-tuples from one domain (dates) to another (timestamps). Unfortunately, it looks like jQuery.map auto-flattens the two-tuples I return, and I don't see a do_not_flatten parameter.

Am I missing something else in the library that won't auto-flatten?

Addendum: I assume that I shouldn't be using Array.map, which does not auto-flatten, because it is JavaScript 1.6. As I understand it, jQuery is supposed to abstract away the JavaScript version I'm running for compatibility reasons.

like image 423
cdleary Avatar asked Mar 31 '09 22:03

cdleary


2 Answers

I was having the same problem; it turns out you can just return a nested array from within the $.map callback and it won't flatten the outer array:

$.map([1, 2, 3], function(element, index) {   return [ element + 1, element + 2 ]; }); => [2, 3, 3, 4, 4, 5] 

Whereas:

$.map([1, 2, 3], function(element, index) {   return [ [ element + 1, element + 2 ] ]; }); => [[2, 3], [3, 4], [4, 5]] 

Hope that helps!

like image 52
T.J. VanSlyke Avatar answered Sep 20 '22 08:09

T.J. VanSlyke


I'm not really sure what a "two-tuple" is and I'm not really sure I got it right from the Google results I looked at. But did you take a look at jQuery.each?

like image 21
evilpenguin Avatar answered Sep 21 '22 08:09

evilpenguin