Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery中的映射返回平面数组_Javascript_Jquery_Arrays - Fatal编程技术网

Javascript jQuery中的映射返回平面数组

Javascript jQuery中的映射返回平面数组,javascript,jquery,arrays,Javascript,Jquery,Arrays,我想要数组的数组 我的代码: 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(),

我想要数组的数组

我的代码:

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();
但是当我有两个post-entry元素,每个元素有4个图像时,我就有了8个图像的数组。如何获得两个元素(每个元素4个)的数组?为什么我有平面阵列


这是因为文档中说(我的重点):

函数可以返回单个数据项或数据数组 要插入到结果集中的项。如果返回一个数组, 数组中的元素将插入到集合中

因此,
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();

您可以创建一个空数组,然后为每个
post条目
添加一个索引,其中包含
img
对象。不过,我怀疑有更好的方法可以做到这一点:

var image_data = [];
$('.post-entry').each(function(i) {

  image_data[i] = $(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(); 

});

有趣…看起来应该有用。你能做一把小提琴吗(用图像交换其他东西)?试着切换函数调用的顺序,先是img.map,然后是.post-entry.map…@Oliboy50我不能,因为。post-entry是父函数。