Checking if a key exists in a JS object

JavascriptJquery

Javascript Problem Overview


I have the following JavaScript object:

var obj = {
    "key1" : val,
    "key2" : val,
    "key3" : val
}

Is there a way to check if a key exists in the array, similar to this?

testArray = jQuery.inArray("key1", obj);

does not work.

Do I have to iterate through the obj like this?

jQuery.each(obj, function(key,val)){}

Javascript Solutions


Solution 1 - Javascript

Use the in operator:

testArray = 'key1' in obj;


Sidenote: What you got there, is actually no jQuery object, but just a plain JavaScript Object.

Solution 2 - Javascript

That's not a jQuery object, it's just an object.

You can use the hasOwnProperty method to check for a key:

if (obj.hasOwnProperty("key1")) {
  ...
}

Solution 3 - Javascript

var obj = {
    "key1" : "k1",
    "key2" : "k2",
    "key3" : "k3"
};

if ("key1" in obj)
    console.log("has key1 in obj");

=========================================================================

To access a child key of another key

var obj = {
    "key1": "k1",
    "key2": "k2",
    "key3": "k3",
    "key4": {
        "keyF": "kf"
    }
};

if ("keyF" in obj.key4)
    console.log("has keyF in obj");

Solution 4 - Javascript

Above answers are good. But this is good too and useful.

!obj['your_key']  // if 'your_key' not in obj the result --> true

It's good for short style of code special in if statements:

if (!obj['your_key']){
    // if 'your_key' not exist in obj
    console.log('key not in obj');
} else {
    // if 'your_key' exist in obj
    console.log('key exist in obj');
}

Note: If your key be equal to null or "" your "if" statement will be wrong.

obj = {'a': '', 'b': null, 'd': 'value'}
!obj['a']    // result ---> true
!obj['b']    // result ---> true
!obj['c']    // result ---> true
!obj['d']    // result ---> false

So, best way for checking if a key exists in a obj is:'a' in obj

Solution 5 - Javascript

map.has(key) is the latest ECMAScript 2015 way of checking the existance of a key in a map. Refer to this for complete details.

Solution 6 - Javascript

You can try this:

const data = {
  name : "Test",
  value: 12
}

if("name" in data){
  //Found
}
else {
  //Not found
}

Solution 7 - Javascript

the simplest way is

const obj = {
  a: 'value of a',
  b: 'value of b',
  c: 'value of c'
};

if(obj.a){
  console.log(obj.a);
}else{
  console.log('obj.a does not exist');
}

Solution 8 - Javascript

This works for me like a charm. I'm inside a foreach function this didn't work obj.hasOwnProperty("key1") also this "key1" in obj

let $schedule = {lesson:'asd',age:'sad'}
    
$schedules.forEach(function(e) {
    if (e['lesson']) {
        $title = e.lesson.lesson_name;
    } else {
        $title = 'No lesson Attached';
    }
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2065483View Question on Stackoverflow
Solution 1 - JavascriptSirkoView Answer on Stackoverflow
Solution 2 - JavascriptGuffaView Answer on Stackoverflow
Solution 3 - JavascriptFernando_JrView Answer on Stackoverflow
Solution 4 - JavascriptAli HallajiView Answer on Stackoverflow
Solution 5 - JavascriptDiabloView Answer on Stackoverflow
Solution 6 - JavascriptAchsuthanView Answer on Stackoverflow
Solution 7 - JavascriptKhanView Answer on Stackoverflow
Solution 8 - JavascriptRolly CraftView Answer on Stackoverflow