The typescript array contains objects with circular references. How do you get rid of those duplicate objects?

such as the title, read a lot of weight removal methods on the Internet, but they are not suitable for my situation, solve! Thank you!

Apr.09,2022

what exactly does your so-called circular reference object look like?

regular array deweighting, I array to set, and back to array. It is easy to write, but it is not clear whether the performance is good or not

class Greeter {
    greeting: string;
    constructor(message: string) {
        this.greeting = message;
    }
    greet() {
        return "Hello, " + this.greeting;
    }
}

const a = new Greeter('1');
const b = new Greeter('2');
const c = new Greeter('3');

const myArr = [a, a, a, a, b, c, c, c, b, c, c, b, b, b, b, c, c, a, a, a, a];

const mySet = new Set(myArr);

const finalArr = Array.from(mySet);
console.log(finalArr)
Menu