typescript数组中包含有循环引用的对象,要怎么去掉那些重复的对象呢?

如题,在网上看了好多去重的方法,但是都不适合我这种情况,求解!谢谢!

阅读 2.9k
1 个回答

你所谓的循环引用的对象,具体是个什么样子?

常规的数组去重,我就array转set,在转回array。写起来简单,性能好不好不太清楚

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)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进