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

后端 未结 2 2010
萌比男神i
萌比男神i 2021-02-06 21:15

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,

2条回答
  •  一整个雨季
    2021-02-06 21:53

    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!

提交回复
热议问题