我有一组元素:
jQuery.each(_this._elements, function (i, element) {
...
});
如何生成一个数组,其中这些元素按element.offset()分组.top?
答案 0 :(得分:1)
一个手工制作的解决方案
var items = [
{ offset: function() { return { top: 3 }}},
{ offset: function() { return { top: 2 }}},
{ offset: function() { return { top: 3 }}},
{ offset: function() { return { top: 1 }}}
];
var temp = {};
$.each(items, function(i, elem) {
var top = elem.offset().top;
if (temp[top]) {
temp[top].push(elem);
} else {
temp[top] = [elem];
}
})
var result = []; //array of arrays
$.each(temp, function(k, elems) {
result.push(elems)
})
console.log(result);
将items
替换为_this._elements