4

I have one class called tax.

    export class tax {
        private _id: string;
        private _name: string;
        private _percentage: number;`

        constructor(id: string = "", taxName: string = "", percentage: number = 0) {
            this._id = id;
            this._name = taxName;
            this._percentage = percentage;
        }


        public get id(): string {
            return this._id;
        }

        public set id(v: string) {
            this._id = v;
        }
        public get name(): string {
            return this._name;
        }

        public set name(v: string) {
            this._name = v;
        }
        public get percentage(): number {
            return this._percentage;
        }

        public set percentage(v: number) {
            this._percentage = v;
        }

        toString(){
            return this.id;
        }
    }

When I create 2 different objects of this class

    a1: tax = new tax("id","name",4);
    a2: tax = new tax("id","name",4); 

    console.log(a1 === a2); //false
    console.log(a1 == a2); //false

When I give a1 === a2 it should give true. what changes i have to do in my class so it will give a1 === a2 ? What I have to do in tax class? or which method I have to override in tax class?

vishvas chauhan
  • 342
  • 1
  • 4
  • 8
  • *"it should give true"* not it should not. it will be true only for **same** object, so object and its reference. not for two different objects like in your case. because you construct two new objects, they are not the same, even if they might look similar. – dfsq Apr 26 '17 at 12:28
  • my question is **what changes i have to do in my `tax` class when i create two different object with same value THEN ** i compare both object it will give me true. – vishvas chauhan Apr 27 '17 at 04:53
  • You can't do anything so that `===` give you `true` for two different objects. You could however, do it smart and turn Tax class into some sort of Singleton variation when it would not construct new object for same parameters but return previous one. Then `a1 === a2` would be `true`. – dfsq Apr 27 '17 at 08:11
  • If you're okay with using lodash, it provides a method for deep comparison between objects. Just compare with _.isEqual(a1, a2). Docs can be found here: https://lodash.com/docs/4.17.5#isEqual – spencer Mar 14 '18 at 21:00
  • The thing to remember here is that you are checking equality of the reference (memory location) but you are wanting to do a value comparison. – Ken Jun 02 '20 at 16:54
  • They closed this thread and point to a Javascript question with more than 4 pages to read through, but I just came up with a Typescript answer... it even allows you to ignore certain attributes... in the case fields27 and 30... I want on the new obj's that don't already exist, but need to ignore certain attributes. const nameof = (name: Extract): string => name; for (const key in Obj1) { if(Obj2[key]!=undefined && (nameof(key) != 'field27' && nameof(key) != 'field30')) { if(Obj1[key] != Obj2[key]) { alreadyExists = false; break; } – AppDreamer Apr 22 '22 at 23:17

1 Answers1

3

You're comparing two different instances of the same class. Even though the values are the same within the instance, they're two completely separate entities.

For example, if we were to have a People class with a single attribute name. Even if there are two people in the world called John Smith, they're two completely separate people.

Each instance has its own unique identifier.

If you want to check if two taxes have the exact same values, you could check them one by one.

console.log(a1.getId() === a2.getId() && a1.getName() === a2.getName() && a1.getPercentage() === a2.getPercentage()) // true

More information about comparing objects can be found here

Community
  • 1
  • 1
Toby Mellor
  • 8,093
  • 8
  • 34
  • 58
  • I used this one with success (to avoid writing manual comparisons): https://www.npmjs.com/package/deep-equal – milosmns Jan 02 '20 at 21:10