-
-
Save reedjones/5ddfbdb193ce0a4dc7489594550eb095 to your computer and use it in GitHub Desktop.
How to get all variables and search through their values in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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