Skip to content

Instantly share code, notes, and snippets.

@vneri
Last active August 4, 2024 07:49
Show Gist options
  • Save vneri/43e41985f2fd311f91a2f1c0cf6a6024 to your computer and use it in GitHub Desktop.
Save vneri/43e41985f2fd311f91a2f1c0cf6a6024 to your computer and use it in GitHub Desktop.
How to get all variables and search through their values in JavaScript
function getAllVariables(){
var searchVarByValue = function(searchString){
var values = Object.keys(this.ValueToVar);
var findings = [];
for (var i=0; i< values.length; i++){
if (values[i] == undefined)
continue;
var position = values[i].toLowerCase().indexOf(searchString.toLowerCase());
if (position != -1){
findings.push({ 'variable': this.ValueToVar[values[i]], 'value' : values[i] });
}
}
return findings;
}
var returnObject = {};
returnObject.VarToValue = [];
returnObject.ValueToVar = [];
returnObject.Vars = [];
returnObject.Values = [];
returnObject.search = searchVarByValue;
for (var variableName in this) {
try{
var value = JSON.stringify(this[variableName]);
returnObject.VarToValue[variableName] = value;
if (this[variableName] != null){
returnObject.ValueToVar[value] = variableName;
returnObject.Values.push(value);
}
returnObject.Vars.push(variableName);
} catch{
continue;
}
}
return returnObject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment