Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Javascript Map has an object key

Looking at a couple of different docs, all I see is when the Map (ECMAScript6) key is a boolean, string, or integer. Is there a way we could use another customized Object (called with the new CustomObject(x,y) constructor call) to be added as a key?

I am able to add an object as a key, but unable to check if the Map has the said object.

var myMap = new Map();
myMap.set( new Tuple(1,1), "foo");
myMap.set('bar', "foo");


myMap.has(?);  
myMap.has('bar');  // returns true

Is there a way around this?

  var myMap = new Map();
  myMap.set( new Tuple(1,1), "foo");

 for(some conditions) {
 var localData = new Tuple(1,1); //Use directly if exists in myMap? 
 map.has(localData) // returns false as this is a different Tuple object. But I need it to return true
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has

like image 360
Loser Coder Avatar asked Jul 06 '17 18:07

Loser Coder


People also ask

How can I tell if a Map has a key?

containsKey() method is used to check whether a particular key is being mapped into the HashMap or not. It takes the key element as a parameter and returns True if that element is mapped in the map.

How do I find the object key on a Map?

To get the keys of a Map object, you use the keys() method. The keys() returns a new iterator object that contains the keys of elements in the map.

How do you check if a key exists in an object?

There are mainly two methods to check the existence of a key in JavaScript Object. The first one is using “in operator” and the second one is using “hasOwnProperty() method”. Method 1: Using 'in' operator: The in operator returns a boolean value if the specified property is in the object.

How do you check if a key is present in an array of objects JavaScript?

Using hasOwnProperty() function The function hasOwnProperty() will check for the existence of a key in the given object and returns true if the key is present or else it returns false. This function takes the key of the object as the parameter and returns the Boolean result accordingly.


1 Answers

You just have to save the reference to the object:

var myMap = new Map();
var myKey = new Tuple(1,1);
myMap.set( myKey, "foo");
myMap.set('bar', "foo");

myMap.has(myKey);           // returns true;  myKey === myKey
myMap.has(new Tuple(1,1));  // returns false; new Tuple(1,1) !== myKey
myMap.has('bar');           // returns true;  'bar' === 'bar'

Edit: Here is how to use an object to achieve what you want, which is to compare objects by their values rather than by reference:

function Tuple (x, y) {
  this.x = x;
  this.y = y;
}
Tuple.prototype.toString = function () {
  return 'Tuple [' + this.x + ',' + this.y + ']';
};

var myObject = {};
myObject[new Tuple(1, 1)] = 'foo';
myObject[new Tuple(1, 2)] = 'bar';
console.log(myObject[new Tuple(1, 1)]); // 'foo'
console.log(myObject[new Tuple(1, 2)]); // 'bar'

These operations will run in constant time on average, which is much faster than searching through a Map for a similar object key in linear time.

like image 93
sbking Avatar answered Oct 13 '22 00:10

sbking