有没有自动变平的jQuery map实用程序吗? [英] Is there a jQuery map utility that doesn't automically flatten?
问题描述
我正在将一个由两个域组成的数组从一个域(日期)映射到另一个域(时间戳).不幸的是,它看起来像jQuery.map
自动展平了我返回的两个元组,并且我没有看到do_not_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?
附录:我假设我不应该使用不会自动变平的Array.map
,因为它是
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.
推荐答案
我遇到了同样的问题;事实证明,您可以只从$ .map回调内部返回嵌套数组,而不会使外部数组变平坦:
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]
位置:
$.map([1, 2, 3], function(element, index) {
return [ [ element + 1, element + 2 ] ];
});
=> [[2, 3], [3, 4], [4, 5]]
希望有帮助!
这篇关于有没有自动变平的jQuery map实用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!