0
class Student{
//Regestration no, Name, semester, degree
constructor(regNo, name, degree, semeter) {
    this.regNo = regNo;
    this.name = name;
    this.degree = degree;
    this.semeter = semeter;
}
}
let studentsSet = new Set();
studentsSet.add(new Student("FA20-BCS-084","Muhammad Abdullah","BSCS","5th"));
studentsSet.add(new Student("FA20-BCS-084","Muhammad Abdullah","BSCS","5th"));
console.log(studentsSet);

**output**

Set(2) {
  Student {
    regNo: 'FA20-BCS-084',
    name: 'Muhammad Abdullah',
    degree: 'BSCS',
    semeter: '5th'
  },
  Student {
    regNo: 'FA20-BCS-084',
    name: 'Muhammad Abdullah',
    degree: 'BSCS',
    semeter: '5th'
  }
}

Although both objects are having same values, but I want to compare both objects on the basis of regNo field, and definitely, both fields are the same in this case So, I want to get only one object in the Set. but the question is how? like we have a concept of hashcode and equals in java. is there anything similar to that in JS?

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
Abdullah
  • 1
  • 1
  • 2
    I believe it's returning both because they are distinct Objects, despite having the same values. In your real case, how are the Students generated? You might consider an Object with the regNo as the key. – mykaf Oct 24 '22 at 15:36

0 Answers0