Fernando Maymone
Fernando Maymone

Reputation: 755

How can I check if a value exists in Map in JavaScript?

It looks a very easy question but I haven't found it anywhere.

How can I know If a value exists in a Map?

For example:

A = [1,2,3,5,6,7]
var myMap = new Map();
for (let i = 0; i < A.length; i++) {
    myMap.set(i,A[i]);
}
for (let z = 1; z < Number.MAX_SAFE_INTEGER; z++) {
    console.log(z);
    if(!myMap.hasValue(z)){
        return z;
    }
}

I want to check if, given one value, this value is on the hash. Like a hasValue.

Upvotes: 31

Views: 77819

Answers (7)

Alikper İslam
Alikper İslam

Reputation: 11

I usually use this approach in Node.js and it works very well. you can check if the value you are looking for exists in a map.

const TableNames = {
        table1: 'oxford',
        table2: 'stanford',
    }

After creating the map this is the code blog that checks if the value you are looking for exists.

if (!Object.values(tableNames).includes(category)) {
    reject(new Error('Invalid category name: ' + category));
    return;
}

Upvotes: 0

Mehdi
Mehdi

Reputation: 1126

function hasValue(map, value) {
    for (let val of map.values()) {
        if (val === value) {
            return true;
        }
    }
    return false;
}

// Example usage:
const A = [1,2,3,5,6,7];
const myMap = new Map();
for (let i = 0; i < A.length; i++) {
    myMap.set(i, A[i]);
}

for (let z = 1; z < Number.MAX_SAFE_INTEGER; z++) {
    console.log(z);
    if (!hasValue(myMap, z)) {
        console.log(`The first missing value is: ${z}`);
        break; // Exit the loop if a missing value is found
    }
}

Upvotes: 1

Bergi
Bergi

Reputation: 664385

You cannot, other than by searching through it:

myMap.values().some(v => v === val) // https://tc39.es/proposal-iterator-helpers
Array.from(myMap.values()).includes(val)
new Set(myMap.values()).has(val)

Use an appropriate data structure instead of the Map, like a set of all the values, in the first place:

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.

Upvotes: 31

Wiktor Kujawa
Wiktor Kujawa

Reputation: 619

For me it works:

Object.values(myMap).includes(myValue);

Upvotes: 10

Johannes Nielsen
Johannes Nielsen

Reputation: 177

I know this question is already answered, but I would like to point out a way to do this without (explicitly) iterating of the entries. You can do

let myMap = new Map([[0, 1], [1, 2], [2, 3], [3, 5], [4, 6], [5, 7]])

for (let z = 1; z < Number.MAX_SAFE_INTEGER; ++z) {
    console.log(z);
    if([...myMap.values()].includes(z) === false){
        return z;
    }
}

So, you could create a function

const mapContainsElement = (map, val) => [...map.values()].includes(v)

Upvotes: -1

Alan Yong
Alan Yong

Reputation: 1033

I, personally prefers to find, indexOf and findIndex.

const array1 = (map, val) => {
  var foo;
  array2.find( (x) => foo = x === foo.id )
  return foo;
}

findIndex

find

indexOf

Upvotes: 0

Dario
Dario

Reputation: 6280

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;
}

Upvotes: 18

Related Questions