我想要数组的数组。
我的代码:
image_data = $('.post-entry').map(function() {
return $(this).find('img').map(function() {
var self = $(this);
return {
big: self.hasClass('big') || self.hasClass('medium'),
offset: self.offset(),
width: self.width(),
height: self.height()
};
}).get();
}).get();
但是当我有两个后输入元素并且每个元素有 4 个图像时,我有 8 个图像的数组。如何获得两个元素组成的数组,每个元素有 4 个元素?为什么我得到平面数组?
最佳答案
这是因为 map() 的文档说(强调我的):
The function can return an individual data item or an array of data items to be inserted into the resulting set. If an array is returned, the elements inside the array are inserted into the set.
因此,map()
会展平您返回的数组,这种行为是设计使然。
尝试将这些数组包装到另一个数组中,因为我相信 map()
只会展平一次:
image_data = $('.post-entry').map(function() {
return [
$(this).find('img').map(function() {
var self = $(this);
return {
big: self.hasClass('big') || self.hasClass('medium'),
offset: self.offset(),
width: self.width(),
height: self.height()
};
}).get()
];
}).get();
关于javascript - map map 在jQuery中返回平面数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19160264/