0

I'm trying to remove obects from an object if they appear in other objects. Really hard to exaplin! Here's an example. I have 2 Objects containing DOM image objects and I would like the DOM image objects removed from the first object if they appear in the second object.

First Object

{
    "241": [{
        "img": image_object_1
    },
    {
        "img": image_object_2
    },
    {
        "img": image_object_3
    },
    {
        "img": image_object_4
    }]
}

Second Object

{
    "241": [{
        "img": image_object_1
    },
    {
        "img": image_object_3
    },
    {
        "img": image_object_4
    }]
}

Expected result of object 1

{
    "241": [{
        "img": image_object_2
    }]
}

I have everything in a single object like so but I'm happy to change the format if needs be

{
    "0": {

    },
    "1": {
        "241.14999389648438": [{
            "img": {
                image_object_1
            },
        },
        {
            "img": {
                image_object_2
            },
        },
        {
            "img": {
                image_object_3
            },
        },
        {
            "img": {
                image_object_4
            },
        }]
    },
    "2": {
        "241.14999389648438": [{
            "img": {
                image_object_2
            },
        },
        {
            "img": {
                image_object_3
            },
        },
        {
            "img": {
                image_object_4
            },
        }]
    }
}

My working code is here

jQuery.fn.reverse = [].reverse;

function same_height(){
    var imob = {};
    var groups = [];
    var heights = [];
    var tp = 0;
    var img = false;
    $("#ez-container .row").each(function(gi){
        imob = {};
        groups[gi] = {};
        heights[gi] = {};
        tp = 0;
        img = false;
        $(this).find(".ez-image img").each(function(){
            img = $(this);
            tp = img.offset().top;

            imob = {
                "img":img,
                "padding":img.outerHeight(true) - (parseInt(img.css('borderBottomWidth'))+parseInt(img.css('borderTopWidth'))) - img.innerHeight()
            };
            if(typeof(groups[gi][tp])=="undefined"){
                groups[gi][tp] = [];
                heights[gi][tp] = [];
            }
            groups[gi][tp].push(imob);
            heights[gi][tp].push(img.height());
        });
    });
    heights.reverse();
    var max_group_height = 0;
    $.each(groups.reverse(),function(gix,grp){
        $.each(grp,function(t,im){
            if(im.length>1){
                $.each(im,function(i,v){
                    max_group_height = Math.max.apply(Math, heights[gix][t]);
                    if(typeof(v.img.attr("data-fixed"))=="undefined"){
                        v.img.css({"height":max_group_height+(v.padding)+"px"}).attr("data-height",0).attr("data-width",0).attr("data-fixed",1);
                    }
                });
            }
        });
    });
    do_swap_images();
}
Andy Gee
  • 3,149
  • 2
  • 29
  • 44
  • Check this: http://stackoverflow.com/questions/1187518/javascript-array-difference – Vinit Prajapati Jun 19 '14 at 09:53
  • I could do it with arrays easily but objects are not the same. – Andy Gee Jun 19 '14 at 09:56
  • Looking at your object's structure, it looks like it could be simplified a lot. For example, why do you keep an array of objects with a single key (and the value being the image)? You could just have an array of the image nodes directly. Also, this `{ '0' : { ...}, '1' : { ... } }` looks very much like you are setting incrementing indexes and could just use an array instead. This would make iterating across the values a lot easier. – christian314159 Jun 19 '14 at 10:18
  • I was just reaching the same conclusiom. I'm looking at creating an array for the root then cycling through it from the bottom up to collect objects to remove. The objects within contain a lot of data though and have been stripped down for brevity here. – Andy Gee Jun 19 '14 at 10:22
  • I was able to revese cycle the array to add a `data-fixed` attribute which is removed in the `do_swap_images()` function, thanks for the tip. Also thanks @rab for helping me look from a different perspective. – Andy Gee Jun 19 '14 at 10:54

1 Answers1

0

if you checking dom image node, you need isSameNode functions. I am not sure about your requirements, hope below code will helps

//suppose a, b are your objects

var key = 241
var diff = a[key].filter( function( v ){
    var firstImgNode = v.img;
    return !b[key].some( function( v ){
        return v.img.isSameNode( firstImgNode  );
    });
});

or if you checking other data type, then simply do v.img == firstImgNode

rab
  • 4,134
  • 1
  • 29
  • 42