0

I have a function designed to sort / order / uniqify a given array.

Here is some of the code: Bubble Sort -

function sort(postsCollection, type, direction){
    let target = postsCollection[0][type];
    let swapp = false,
        n = postsCollection[0].length - 1,
        x = postsCollection[0];
    do {
        swapp = false;
        for(let i = 0; i < n; i++){
            if(x[i][type] < x[i+1][type]){
                x[i+1] = x[i];
                swapp = true;
            }
        }
        n--;
    } while (swapp);
    return x;
}

These exist in module.exports:

    orderPostsCollection(postsCollection, sortType, direction, callback){
    // Unique Array Creater
    let uniq = a => [...new Set(a)];

    if(!sortType && !direction){
        callback([...new Set(postsCollection)]);
    }

    if(sortType) {
        callback(sort([...new Set(postsCollection)], sortType, direction));
    }else if(direction){
        callback(sort([...new Set(postsCollection)], false, direction));
    }
},
uniqify(postsCollection, callback){
    console.log([...new Set(postsCollection)]);
    callback([...new Set(postsCollection)]);
}

it should make the data array unique. But it isn't..

Here is a sample:

{"posts":[{"author":"Rylee Paul","authorId":9,"id":1,"likes":960,"popularity":0.13,"reads":50361,"tags":["tech","health"]},{"author":"Zackery Turner","authorId":12,"id":2,"likes":469,"popularity":0.68,"reads":90406,"tags":["startups","tech","history"]},{"author":"Zackery Turner","authorId":12,"id":2,"likes":469,"popularity":0.68,"reads":90406,"tags":["startups","tech","history"]},{"author":"Zackery Turner","authorId":12,"id":2,"likes":469,"popularity":0.68,"reads":90406,"tags":["startups","tech","history"]},{"author":"Elisha Friedman","authorId":8,"id":13,"likes":230,"popularity":0.31,"reads":64058,"tags":["design","tech"]},{"author":"Elisha Friedman","authorId":8,"id":13,"likes":230,"popularity":0.31,"reads":64058,"tags":["design","tech"]},{"author":"Elisha Friedman","authorId":8,"id":13,"likes":230,"popularity":0.31,"reads":64058,"tags":["design","tech"]},{"author":"Elisha Friedman","authorId":8,"id":13,"likes":230,"popularity":0.31,"reads":64058,"tags":["design","tech"]},{"author":"Elisha Friedman","authorId":8,"id":13,"likes":230,"popularity":0.31,"reads":64058,"tags":["design","tech"]},{"author":"Elisha Friedman","authorId":8,"id":13,"likes":230,"popularity":0.31,"reads":64058,"tags":["design","tech"]},{"author":"Elisha Friedman","authorId":8,"id":13,"likes":230,"popularity":0.31,"reads":64058,"tags":["design","tech"]},{"author":"Elisha Friedman","authorId":8,"id":13,"likes":230,"popularity":0.31,"reads":64058,"tags":["design","tech"]},{"author":"Adalyn Blevins","authorId":11,"id":37,"likes":107,"popularity":0.55,"reads":35946,"tags":["tech","health","history"]},{"author":"Adalyn Blevins","authorId":11,"id":37,"likes":107,"popularity":0.55,"reads":35946,"tags":["tech","health","history"]},{"author":"Jon Abbott","authorId":4,"id":46,"likes":89,"popularity":0.96,"reads":79298,"tags":["culture","tech"]},{"author":"Jon Abbott","authorId":4,"id":46,"likes":89,"popularity":0.96,"reads":79298,"tags":["culture","tech"]},{"author":"Jon Abbott","authorId":4,"id":46,"likes":89,"popularity":0.96,"reads":79298,"tags":["culture","tech"]},{"author":"Jon Abbott","authorId":4,"id":46,"likes":89,"popularity":0.96,"reads":79298,"tags":["culture","tech"]},{"author":"Jon Abbott","authorId":4,"id":46,"likes":89,"popularity":0.96,"reads":79298,"tags":["culture","tech"]},{"author":"Jon Abbott","authorId":4,"id":46,"likes":89,"popularity":0.96,"reads":79298,"tags":["culture","tech"]},{"author":"Jon Abbott","authorId":4,"id":46,"likes":89,"popularity":0.96,"reads":79298,"tags":["culture","tech"]},{"author":"Jon Abbott","authorId":4,"id":46,"likes":89,"popularity":0.96,"reads":79298,"tags":["culture","tech"]},{"author":"Jon Abbott","authorId":4,"id":46,"likes":89,"popularity":0.96,"reads":79298,"tags":["culture","tech"]},{"author":"Bryson Bowers","authorId":6,"id":85,"likes":25,"popularity":0.18,"reads":16861,"tags":["tech"]},{"author":"Bryson Bowers","authorId":6,"id":85,"likes":25,"popularity":0.18,"reads":16861,"tags":["tech"]},{"author":"Bryson Bowers","authorId":6,"id":85,"likes":25,"popularity":0.18,"reads":16861,"tags":["tech"]},{"author":"Bryson Bowers","authorId":6,"id":85,"likes":25,"popularity":0.18,"reads":16861,"tags":["tech"]},{"author":"Bryson Bowers","authorId":6,"id":85,"likes":25,"popularity":0.18,"reads":16861,"tags":["tech"]}]}

why is this array is not made unique? what gives the above result?

SuleymanSah
  • 13,055
  • 5
  • 17
  • 42

1 Answers1

0

You're assuming [...new Set(data)] will filter out duplicate values – which is basically correct – but: Sets only filter out duplicates of primitive values (strings, numbers, booleans, etc.). Objects are not considered equal to each other even if they have the exact same content.

You could instead call your own uniqueFilter(postsCollection) function instead of using [...new Set(postsCollection)] that does filter objects based on the sameness of their content (see here for some examples).


Objects are only considered "equal" to each other when they're literally references to the same object. e.g.

const a = {};
a === {}; // false

const b = a;
a === b; // true (because `b` is literally pointing to the exact same object as `a`)

Think of every object you make as having a secret key that always makes it unique regardless of its content.

SamVK
  • 2,388
  • 1
  • 10
  • 17