Skip to content Skip to sidebar Skip to footer

Filter Unique & Duplicate Objects In Array Using Underscore

I have an array of objects: data = [{'origin':'SJU','dest':'JFK','rank':48},{'origin':'JFK','dest':'SJU','rank':21},{'origin':'IAD','dest':'LAX','rank':31},{'origin':'LAS','dest':

Solution 1:

It might be easier to introduce a temporary variable to hold the groups:

var data = [{"origin":"SJU","dest":"JFK","rank":48},{"origin":"JFK","dest":"SJU","rank":21},{"origin":"IAD","dest":"LAX","rank":31},{"origin":"LAS","dest":"SJU","rank":21}]

var groups = _.groupBy(data, function(item) {
  return [item.origin, item.dest].sort();
});

Then:

var duplicates = [],
singles = [];

_.each(groups, function(group) {
  if (group.length > 1) {
    duplicates.push.apply(duplicates, group);
  } else {
    singles.push(group[0]);
  }
});

Demo


Solution 2:

I don't understand your code. However I got the problem.

The way I see it is 2 steps:

  • identify duplicates and push them in an array duplicates
  • create the uniquearray by copying the inital array and remove all the elements that match duplicates array.

Maybe it has lower performance but it could be clearer.


Solution 3:

Here is the algorithm using plain javascript. It is probably not the most efficient one if you will have thousands of records, but it get's the job done.

var data = [
    {"origin": "SJU", "dest": "JFK", "rank":48},
    {"origin": "JFK", "dest": "SJU", "rank":21},
    {"origin": "IAD", "dest": "LAX", "rank":31},
    {"origin": "LAS", "dest": "SJU", "rank":21}
];

var uniques = [];
var doubles = [];

var i, j, l = data.length, origin, dest, foundDouble;

for ( i = 0; i < l; i += 1 ) {
    origin = data[i].origin;
    dest = data[i].dest;
    foundDouble = false;

    for ( j = 0; j < l; j += 1 ) {
        //skip the same row
        if ( i == j ) {
            continue;
        }

        if ( (data[j].origin == origin || data[j].origin == dest) && (data[j].dest == origin || data[j].dest == dest) ) {
            doubles.push( data[i] );
            foundDouble = true;
        }
    }

    if ( !foundDouble ) {
        uniques.push( data[i] );
    }
}

console.log( 'Uniques', uniques );
console.log( 'Doubles', doubles );

Solution 4:

There are many ways to do this, but here's one. I never used underscore, but the algorithm is pretty straight forward and you should be able to convert it easily.

var voyages = [{"origin":"SJU","dest":"JFK","rank":48},{"origin":"JFK","dest":"SJU","rank":21},{"origin":"IAD","dest":"LAX","rank":31},{"origin":"LAS","dest":"SJU","rank":21}],
    dupes = [],
    uniques = [],
    countMap = new Map(), //Map is from Harmony, but you can use a plain object
    voyageKeyOf = function (voyage) { return [voyage.origin, voyage.dest].sort().join(''); },
    uniques;

//Create a map that stores how many times every voyage keys were seen
voyages.forEach(function (voyage) {
    var key = voyageKeyOf(voyage),
        hasCount = countMap.get(key);

    if (!hasCount) countMap.set(key, 1);
    else {
        let count = countMap.get(key);
        countMap.set(key, ++count);
    }
});

voyages.forEach(function (voyage) {
    var key = voyageKeyOf(voyage),
        isUnique = countMap.get(key) == 1;

    if (isUnique) uniques.push(voyage)
    else dupes.push(voyage);
});


console.log('uniques', uniques);
console.log('dupes', dupes);

Post a Comment for "Filter Unique & Duplicate Objects In Array Using Underscore"