-1

I'm struck with some problem.

Here is what i'm trying:

can i achieve this:

var case = 'toUpperCase()';

'abcd'.case;  //output ===>  ABCD

user will pass case uppercase or lowercase

function getIndex(obj){
    var index = window[String('someGlobalArray')].indexOf(String(obj.name).case);}
    var pass = {name:'helper',case:'toUpperCase'}; 
    someGlobalArray =  ['HELPER','A','b','C',.......]; 
    getIndex(pass);
Liam
  • 27,717
  • 28
  • 128
  • 190
EaBengaluru
  • 131
  • 2
  • 17
  • 59
  • Please be clearer about **what** you are trying to achieve, not how you tried to do it. – Ozzy Walsh Aug 28 '18 at 08:32
  • `var case = 'toUpperCase()'; 'abcd'.case; //output ===> ABCD` No, not possible like that. Though, if you were to use bracket notation instead, and called *outside* of the string, it could be – CertainPerformance Aug 28 '18 at 08:32
  • Possible duplicate of [How to execute a JavaScript function when I have its name as a string](https://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string) – Liam Aug 28 '18 at 08:33
  • 1
    How can this be node and Jquery? – Liam Aug 28 '18 at 08:33

1 Answers1

5

This is possible, by using square bracket notation to access the function:

const caseFunction = 'toUpperCase'

const result = 'abcd'[caseFunction]()

console.log(result)

You've edited the question since I answered, I'll see if I can make sense of what you're asking

OliverRadini
  • 6,238
  • 1
  • 21
  • 46