Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
63
rated 0 times [  66] [ 3]  / answers: 1 / hits: 25362  / 11 Years ago, tue, january 28, 2014, 12:00:00

I need to store a list of points and check if a new point is already included in that list



class Point {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}

window.onload = () => {
var points : Point[] = [];
points.push(new Point(1,1));
var point = new Point(1,1);
alert(points.indexOf(point)); // -1
}


Obviously typescript uses comparison by reference but in this case that doesn't make sense. In Java or C# I would overload the equals method, in typescript that doesn't seem to be possible.



I considered to loop through the array with foreach and check each entry for equality, but that seems rather complicated and would bloat the code.



Is there something like equals in typescript ? How can I implement my own comparisons ?


More From » web

 Answers
37

You could wrap the collection in a PointList that only allows Point objects to be added via an add method, which checks to ensure no duplicates are added.



This has the benefit of encapsulating the No duplicates rule in a single place, rather than hoping that all calling code will check before adding a duplicate, which would duplicate the rule in many places.



class Point {
constructor(public x: number, public y: number) {
}
}

class PointList {
private points: Point[] = [];

get length() {
return this.points.length;
}

add(point: Point) {
if (this.exists(point)) {
// throw 'Duplicate point';
return false;
}

this.points.push(point);
return true;
}

exists(point: Point) {
return this.findIndex(point) > -1;
}

findIndex(point: Point) {
for (var i = 0; i < this.points.length; i++) {
var existingPoint = this.points[i];
if (existingPoint.x === point.x && existingPoint.y === point.y) {
return i;
}
}

return -1;
}
}

var pointList = new PointList();

var pointA = new Point(1, 1);
var pointB = new Point(1, 1);
var pointC = new Point(1, 2);

pointList.add(pointA);
alert(pointList.length); // 1

pointList.add(pointB);
alert(pointList.length); // 1

pointList.add(pointC);
alert(pointList.length); // 2

[#72895] Monday, January 27, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josefn

Total Points: 251
Total Questions: 93
Total Answers: 84

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
;