Given:
JavaScript
x
2
1
var dic = {1: 11, 2: 22}
2
How to test if (1, 11) exists?
Advertisement
Answer
Most of the time very simply, with
JavaScript
1
2
1
if (dic[1] === 11)
2
with one caveat: if the value you are looking for is undefined
this will not do because it cannot distinguish between { 1: undefined }
and just {}
. In that case you need the more verbose test
JavaScript
1
2
1
if ('1' in dic && dic[1] === undefined)
2