Skip to content

Instantly share code, notes, and snippets.

@eat-sleep-code
Last active August 15, 2020 04:35
Show Gist options
  • Save eat-sleep-code/f7f19012b728a2a5725a740bdeb1319c to your computer and use it in GitHub Desktop.
Save eat-sleep-code/f7f19012b728a2a5725a740bdeb1319c to your computer and use it in GitHub Desktop.
Map String to Index
var mappingIndex = 0;
var mappingDictionary = [];
function MappingDictionary(stringToFind) {
output = 0;
var queryResult = mappingDictionary.filter(obj => Object.values(obj).some(val => val?val.toString().toLowerCase().includes(stringToFind):false))[0];
if (queryResult) {
output = queryResult["Key"];
}
else {
mappingIndex = mappingIndex + 1;
var mappingDictionaryEntry = {};
mappingDictionaryEntry.Key = mappingIndex;
mappingDictionaryEntry.Value = stringToFind;
mappingDictionary.push(mappingDictionaryEntry);
output = mappingIndex;
}
console.log(stringToFind + ": " + output);
return output;
}
// var uniqueValue = inputIndex + "-" + inputValue;
//var output = MappingDictionary(uniqueValue);
MappingDictionary("dog");
MappingDictionary("bird");
MappingDictionary("cat");
MappingDictionary("dog");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment