JavaScript Function That Returns The VARIABLE NAME Of A TYPE BOOLEAN Instead Of The VALUE Itself Inside Template Literal
is there a function in 'javascript' where I can make my variables(not the value) as strings like parseVariableName something? for example, in my scenario I want to have a **dynamic
Solution 1:
Not really sure what exactly you are trying to achieve here. But to get your expected result, the following is what I can think of: You can put all your variables in an object instead of having it in the default window
object. Here I am using prop
. That way you can just pass the whole object inside a function where you check where the first true
value occurs (which behaves almost like the OR chain in your example). And then return the key
of the true value, which can be then used as a class. Below is a working example.
let prop = {
isPivotable: true,
isTable: false,
isGrid: false
}
function doCompare(prop) {
let found = '';
(Object.keys(prop)).some(x => {
if (prop[x]) {
found = x;
return true;
}
})
return found;
}
const cn = `myClass--sample ${doCompare(prop)}`
console.log(cn);
Post a Comment for "JavaScript Function That Returns The VARIABLE NAME Of A TYPE BOOLEAN Instead Of The VALUE Itself Inside Template Literal"