How to check string key is available in a JavaScript object
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Monday
Hi All,
How to check system property stringKeys are available in Object Or Not ?
I have below Object & Property :
var myObject = {
"name": "ServiceNow",
"version": "Quebec",
"Year":"2025",
"Country":"Canada"
};
system property: StringKeys
name, year
// ---I tried below script which is working fine--//
var fields = gs.getProperty("StringKeys").split(',');
for(var key in fields) {
if(fields[key] in myObject) {
gs.info("Success ");
}
}
Can you help me is there better way to check string keys available in Object Or not ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Monday
Hi @chanikya ,
Use Array.prototype.forEach() and the in Operator....
Instead of using a for...in over the array of field names, you can make the check cleaner and more semantic using forEach:
var myObject = {
name: "ServiceNow",
version: "Quebec",
Year: "2025",
Country: "Canada"
};
var fields = gs.getProperty("StringKeys").split(',');
fields.forEach(function(key) {
if (key in myObject) {
gs.info("Key '" + key + "' exists in object");
} else {
gs.info("Key '" + key + "' does NOT exist in object");
}
});
This is more preferable because (in) operator ensures you're checking for the actual key presence, not just value existence. It safely returns true even if the value is undefined....Using forEach avoids ambiguity that can arise from for...in when iterating over arrays.
If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
can we use below format ? if(fields[key] in myObject)
var fields = gs.getProperty("StringKeys").split(',');
for(var key in fields) {
if(fields[key] in myObject) {
what is the benefit if we use below
myObject.hasOwnProperty(fields[key]);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Monday
Hi @chanikya ,
You can use .hasOwnProperty for slightly more specific task by passing the property name as argument.
Here in your case, you can pass 'key' as argument
if (myObject.hasOwnProperty(key)) {
gs.info("\n Result (using hasOwnProperty): The key '" + key + "' is a direct property of the object.");
}
else
gs.info("\n Result (using hasOwnProperty): The key '" + key + "' is not direct property of the object.");
Thanks,
Bhimashankar H
-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
can we use below format ? if(fields[key] in myObject)
var fields = gs.getProperty("StringKeys").split(',');
for(var key in fields) {
if(fields[key] in myObject) {
what is the benefit if we use below
myObject.hasOwnProperty(fields[key]);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hey @chanikya ,
That code snippet will work same, for loop iterate through the indices of the array and fields[key] correctly retrieves the string value to be checked.
The main benefit of using myObject.hasOwnProperty(fields[key]) is precision. It checks if a property exists directly on your object and ignores any properties that might be inherited from a parent object (its prototype).
You can understand like in Vs hasOwnProperty
in Operator: it is like asking "Does this apartment have access to a swimming pool ?" The answer could be yes, because the building complex has a pool that all resident can use(an inherited property)
hasOwnProperty: it is like asking "Does this apartment have its own private swimming pool inside it?". This checks only features that belong to directly that specific own property/apartment.
Using hasOwnProperty() is a safer, more robust habit for general-purpose JavaScript development, as it prevents accidentally acting on inherited properties.
Thanks,
Bhimashankar H
-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!