Javascript 我的唯一函数在阵列上不起作用?

Javascript 我的唯一函数在阵列上不起作用?,javascript,arrays,node.js,set,unique,Javascript,Arrays,Node.js,Set,Unique,我有一个函数,用于对给定数组进行排序/排序/uniqify 下面是一些代码: 气泡排序- function sort(postsCollection, type, direction){ let target = postsCollection[0][type]; let swapp = false, n = postsCollection[0].length - 1, x = postsCollection[0]; do {

我有一个函数,用于对给定数组进行排序/排序/uniqify

下面是一些代码: 气泡排序-

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;
}
它应该使数据数组唯一。但事实并非如此

以下是一个示例:

{"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"]}]}

为什么这个数组不是唯一的?是什么给出了上述结果?

您假设
[…新集合(数据)]
将过滤掉重复的值–这基本上是正确的–但是:
Set
s只过滤掉重复的(字符串、数字、布尔值等)。即使对象具有完全相同的内容,也不认为它们彼此相等✝.

您可以调用自己的
uniqueFilter(postsCollection)
函数,而不是使用
[…new Set(postsCollection)]
,该函数根据对象内容的相同性过滤对象(请参阅)


✝ 只有当对象在字面上是对同一对象的引用时,才认为它们彼此“相等”。e、 g

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

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

将您创建的每个对象都视为具有一个始终使其唯一的密钥,而不管其内容如何。

我最近发现,
[…新集合(arr)]
不会删除对象,因为它们被视为object:object
const a = {};
a === {}; // false

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