4

I have an array of nested object (I use this to populate my tree), something like below..

var obj1= {
        text: "TreeRoot",
        items: [ {
            text: "subgroup1" ,
            items: [ {
                text: "subgroup2",
                items: [ {
                    text: "subgroup3",
                    items: [ {
                        text: "subgroup4",
                        items: [ {
                            text: "subgroup5"
                        }]
                    }]
                }]
            }]
        }]
    };

var obj2 = {
        text: "TreeRoot",
        items: [ {
            text: "subgroup1" ,
            items: [ {
                text: "subgroup2",
                items: [ {
                    text: "subgroup3",
                    items: [ {
                        text: "subgroup4",
                        items: [ {
                            text: "subgroup5"
                        }]
                    }]
                }]
            }]
        }]
    };


 var obj3= {
        text: "TreeRoot",
        items: [ {
            text: "subgroup1" ,
            items: [ {
                text: "subgroup2"
                }]
            }]
        }]
    };

var finalArray=[];
finalArray.push(obj1);
finalArray.push(obj2);
finalArray.push(obj3);

Now I need to remove the duplicate objects from my final array using text(i.e I should remove obj2 from my array)...

This is what I tried..

var arr = {};

for ( var i=0; i < finalPath.length; i++ )
     arr[finalArray[i]['text']] = finalArray[i];

finalArray= new Array();
for ( key in arr )
    finalArray.push(arr[key]);

Can someone let me know the best possible way?

Edit:

I think the below solution works, but yet to test it completely...

var arr = {};

for ( var i=0; i < finalArray.length; i++ ){
    if(finalArray[i].items){
        for(var j=0;j<finalArray[i].items.length;j++){
            arr[finalArray[i].items[j]['text']] = finalArray[i];
        }
    }else{
        arr[finalArray[i]['text']] = finalArray[i];
    }
}


finalArray= new Array();
for ( key in arr )
    finalArray.push(arr[key])

Thanks, Barani

Learner
  • 2,303
  • 9
  • 46
  • 81

3 Answers3

2

JSON seems to be pretty simple to use, and keeps your code clean, but probably it will use a lot of processing.

There's another way to solve your problem, you can create a recursive function that will go through the array and compare each branch.

function areEqual(a, b) {
    if( (a.items && !b.items) || (!a.items && b.items) ) return false;
    else if(!a.items && !b.items) return a.text == b.text ? true : false;
    else return (a.text == b.text) ? areEqual(a.items[0], b.items[0]) : false;
}

This code works with the data you've provided, but you will probably need to adjust the function if you want to test other values (not only text)

areEqual(obj1, obj2) : true
areEqual(obj1, obj3) : false

Edit:

Here is a simplified version of the function, and even better if you need to compare multiple elements (not only text)

function areEqual(a, b) {
    var conditions = a.text == b.text /* && a.another_var == b.another_var */;

    if( typeof(a.items) != typeof(b.items) ) return false;
    if(a.items && b.items && conditions) return areEqual(a.items[0], b.items[0]);
    return conditions;
}
António Almeida
  • 9,620
  • 8
  • 59
  • 66
1

Sorry but I think your code would fail if objects have a depth higher than 2. At least, the logic seems quite strange. Why don't you define a test to check if two objects are equal ? You can use a recursive test, or simply test the equality on a json version : something like :

function are_equal(obj1,obj2) {
return JSON.stringify(obj1)==JSON.stringify(obj2);
}

and then use the response here Remove Duplicates from JavaScript Array :

uniqueArray = finalArray.filter(function(elem, pos) {
return are_equal(elem,pos);
})

I haven't tested it, hope this helps.

Community
  • 1
  • 1
Pixou
  • 1,719
  • 13
  • 23
0

Try this code:

var ObjList = [......]; /*All your objects*/
var dict = {}; /*Dictionary for duplicate elimination*/
for (var obj in ObjList) 
{
  dict[JSON.stringify(obj)] = JSON.stringify(obj);
}
var newObjList = [];
for (var key in dict)
{
  newObjList.push(key);
}

Now newObjList has all your unique items however deep the tree might be.

DhruvJoshi
  • 17,041
  • 6
  • 41
  • 60
GautamJeyaraman
  • 545
  • 2
  • 11