0

Fiddle Example:

I have an array of objects like this:

var bigarr = 
[ 
  [ { name: 'ABC',id: 391},{ name: 'XYZ',id: 545}],
  [ { name: 'EFG',id: 390},{ name: 'XYZ',id: 545}],
  [ { name: 'XYZ',id: 545},{ name: 'ABC',id: 391}],
  [ { name: 'ABC',id: 391},{ name: 'XYZ',id: 545}]
];

How can I remove any of the duplicated pair of objects that have id545 and 391 to reduce the array to this:

var newbigarr = 
[ 
  [ { name: 'ABC',id: 391},{ name: 'XYZ',id: 545}],
  [ { name: 'EFG',id: 390},{ name: 'XYZ',id: 545}]
];

I have thought of filtering out the duplicated pairs by making a new list of array:

[{391: 391,545: 545},{390: 390,545: 545}]

and then iterating over it and bigarr to build the newbigarr, but my code isn't even able to create that list to begin with.

var test_id = [];
for(var i = 0;i < bigarr.length;i++)
{
   var value_obj = {};
   for(var j in bigarr[i])
   {
     var value = bigarr[i][j]["id"];  
     value_obj[value] = value;
   }
   test_id.push(value_obj);
}

console.log(test_id);

I'm using lodash,so any solution involving lodash is welcomed.

4 Answers 4

1

If you are using underscore, you could do something like this:

var bigarr = 
[ 
  [ { name: 'ABC',id: 391},{ name: 'XYZ',id: 545}],
  [ { name: 'EFG',id: 390},{ name: 'XYZ',id: 545}],
  [ { name: 'XYZ',id: 545},{ name: 'ABC',id: 391}],
  [ { name: 'ABC',id: 391},{ name: 'XYZ',id: 545}]
];

console.log(_.uniq(bigarr,function(item){
    return [
        [item[0].id+item[0].name],
        [item[1].id+item[1].name]
    ].sort().toString()
}))

Edit: this only works if you always have one pair of objects with an id and a string name. Deepened objects or other keys are not checked

Sign up to request clarification or add additional context in comments.

3 Comments

You're making unstated assumptions about the key and value formats.
just using example given
That's a lazy response, you should at least tell people about the holes you're leaving open.
1

JSFIDDLE

If your using pure js, you can do this something like this just using iteration and a secondary array to keep track of what you've seen:

var bigarr = 
[ 
  [ { name: 'ABC',id: 391},{ name: 'XYZ',id: 545}],
  [ { name: 'EFG',id: 390},{ name: 'XYZ',id: 545}],
  [ { name: 'XYZ',id: 545},{ name: 'ABC',id: 391}],
  [ { name: 'ABC',id: 391},{ name: 'XYZ',id: 545}]
];

var seen = [];
var final = [];

for(i = 0; i < bigarr.length; i++){
    var unseen = true;
    for(j = 0;j<seen.length;j++){
        if((seen[j][0] == bigarr[i][0].id && seen[j][1] == bigarr[i][1].id) || (seen[j][0] == bigarr[i][1].id && seen[j][1] == bigarr[i][0].id)
          ){
            unseen = false;
        }
    }
    if(unseen){
        final.push(bigarr[i]);
        seen.push([bigarr[i][0].id, bigarr[i][1].id]);
    }
}

Comments

1

Using Lo-Dash. JSFiddle

var bigarr = 
[ 
  [ { name: 'ABC',id: 391},{ name: 'XYZ',id: 545}],
  [ { name: 'EFG',id: 390},{ name: 'XYZ',id: 545}],
  [ { name: 'XYZ',id: 545},{ name: 'ABC',id: 391}],
  [ { name: 'ABC',id: 391},{ name: 'XYZ',id: 545}]
];

var seenIds = {};
var nodup = _.filter(bigarr, function(pair) {
    var ids = [pair[0].id, pair[1].id].sort();
    if(seenIds[ids]) {
        return false;
    } else {
        seenIds[ids] = true;
        return true;
    }
});

Comments

1

One line solution with lo-dash (jsFiddle):

var bigarr = 
[ 
  [ { name: 'ABC',id: 391},{ name: 'XYZ',id: 545}],
  [ { name: 'EFG',id: 390},{ name: 'XYZ',id: 545}],
  [ { name: 'XYZ',id: 545},{ name: 'ABC',id: 391}],
  [ { name: 'ABC',id: 391},{ name: 'XYZ',id: 545}]
];  

_.uniq(bigarr, function(a) { return _.pluck(a, 'id').sort() + '' });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.