I needed to get the variable name as a string recently, some notes to remember:
// getting variable name as string
// regular string, array, int, object
const cherryshoeTypes1 = "hi";
const cherryshoeTypes2 = ["cherry", "shoe"];
const cherryshoeTypes3 = 9;
const cherryshoeTypes4 = {name:"cherry"};
const dynamicVarName1 = Object.keys({cherryshoeTypes1})[0];
const dynamicVarName2 = Object.keys({cherryshoeTypes2})[0];
const dynamicVarName3 = Object.keys({cherryshoeTypes3})[0];
const dynamicVarName4 = Object.keys({cherryshoeTypes4})[0];
console.log(dynamicVarName1);
console.log(dynamicVarName2);
console.log(dynamicVarName3);
console.log(dynamicVarName4);
An article that was helpful:
https://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript
No comments:
Post a Comment
I appreciate your time in leaving a comment!