typescript set of objects

Discussão em 'Angular' iniciado por mary, Novembro 20, 2024.

  1. mary

    mary Guest

    I am new in angular2 and typescript. I have a problem in creating an unique collection like Set<>. I want to avoid duplicate objects in a collection, for that purpose, try to use a set dataType like following code:

    private cardItems = new Set<MyBean>([]);


    MyBean is an object.

    export class MyBean {

    id:integer
    ownerId:integer
    ownerName:string
    img: string;

    constructor() {

    }
    public equals(obj: MyBean) {
    console.log(obj.id);
    if (this.id == obj.id) {
    console.log(obj.id);
    return true;
    }
    if (obj == null)
    return false;

    return true;
    }

    public hashCode(obj: MyBean) {
    return obj.id
    }


    }

    but equals and hashCode does not run in this way.and I have duplicate objects in set.

    What is the solution for implementing Set?

    Many thanks

    Continue reading...

Compartilhe esta Página