Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,855 questions

51,776 answers

573 users

How to create a map with key type point (x, y) and value type string in JavaScript

1 Answer

0 votes
class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  // Getters
  getX() {
    return this.x;
  }

  getY() {
    return this.y;
  }

  // toString for key representation
  toString() {
    return `(${this.x}, ${this.y})`;
  }

  // Custom key for Map (since JS Maps use reference equality for objects)
  key() {
    return `${this.x},${this.y}`;
  }
}

// Simulate Map<Point, String> using string keys
const map = new Map();

map.set(new Point(2, 7).key(), "A");
map.set(new Point(3, 6).key(), "B");
map.set(new Point(0, 0).key(), "C");

// Print x and y separately
for (const [keyStr, value] of map.entries()) {
    const [x, y] = keyStr.split(',').map(Number);
    console.log(`x: ${x}, y: ${y} => ${value}`);
}



/*
run:
  
x: 2, y: 7 => A
x: 3, y: 6 => B
x: 0, y: 0 => C

*/




answered Aug 10, 2025 by avibootz
...