How can I check if an VALUE exists in MAP on Javascript?

You cannot, other than by searching through it:

Array.from(myMap.values()).includes(val)

Use an appropriate data structure instead, like a set of all the values:

A = [1,2,3,5,6,7]
var myValues = new Set(A);

for (let z = 1; z < Number.MAX_SAFE_INTEGER; z++) {
    console.log(z);
    if(!myValues.has(z)) {
        return z;
    }
}

Of course, given the fact that your A is sorted already, you could iterate it directly to find the lowest missing value.


You can use iterate over the map, look for the value and return true (exiting the loop) as soon as you find it. Or you return false if the element does not exist. Something like:

const findInMap = (map, val) => {
  for (let [k, v] of map) {
    if (v === val) { 
      return true; 
    }
  }  
  return false;
}

Tags:

Javascript