- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2022 09:53 PM
Hi Team,
I have an Array 'arr' and i already have a value as '4', Now I need to fetch only the specific Key from the value (I need 'Yes' from '4' in an array)
arr = {"Yes":"4","Limited":"3","Unvalidated":"2","No":"1","-":"0"}
could anyone please provide there suggention ?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2022 10:03 PM
Hey,
You can use below approach:
This is from geeksforgeeks.
Basically getKeyByValue function we can pass the object that your have and the value you are searching for, it will return you the key.
var exampleObject = {
key1: 'Geeks',
key2: 100,
key3: 'Javascript'
};
ans = gs.info(getKeyByValue(exampleObject, 100));
function getKeyByValue(object, value) {
for (var prop in object) {
if (object.hasOwnProperty(prop)) {
if (object[prop] === value)
return prop;
}
}
}
.Output : key2
Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions 🙂
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2022 10:03 PM
Hey,
You can use below approach:
This is from geeksforgeeks.
Basically getKeyByValue function we can pass the object that your have and the value you are searching for, it will return you the key.
var exampleObject = {
key1: 'Geeks',
key2: 100,
key3: 'Javascript'
};
ans = gs.info(getKeyByValue(exampleObject, 100));
function getKeyByValue(object, value) {
for (var prop in object) {
if (object.hasOwnProperty(prop)) {
if (object[prop] === value)
return prop;
}
}
}
.Output : key2
Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions 🙂
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2022 10:05 PM
Hi,
Its not an array its object. In order to access only value you can try below code
arr = {"Yes":"4","Limited":"3","Unvalidated":"2","No":"1","-":"0"}
gs.log(arr.Yes)
Output:
4
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2022 10:12 PM
Hi,
sample script
var arr = {"Yes":"4","Limited":"3","Unvalidated":"2","No":"1","-":"0"};
var myKeyName = getKey(4,arr);
gs.info(myKeyName);
function getKey(val,array){
for (var key in array) {
if(array[key] == val){
return key;
}
}
return false;
}
Output:
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2022 10:19 PM