为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕娘9325324 2019-01-30 10:25:51
如题,在网上看了好多去重的方法,但是都不适合我这种情况,求解!谢谢!
查看完整描述

1 回答

?
www说

TA贡献1775条经验 获得超8个赞

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

常规的数组去重,我就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)


查看完整回答
反对 回复 2019-01-30
  • 1 回答
  • 0 关注
  • 1644 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号