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?