2

I'm a big noob at JS,so if my question is hard to understand then sorry😂. I'm writing a program in JS(Electron) that provides a user interface for another program I made in C++, so I'm basically rewriting it in JavaScript.

I want to use this JSON variable(or whatever it's called) in my code.

var ShowSecondsInSystemClock = '{"name":"ShowSecondsInSystemClock","Description":"Patches the System Tray clock to show seconds","ExplorerRestartRequired":"true","category":"UI-Tweaks","badges":"UITweaks"}'

Then I would like to use this function where the parameter of the function "ShowSecondsInSystemClock" is.

function TweakParser(TweakName, NeeddedReturn) {
  if (NeeddedReturn == "Description") {
    //I'm trying to use TweakName as the parameter of parse(),but it only 
    //accepts the name of the Tweak directly
    var NeeddedTweakInfo = JSON.parse(TweakName)
    return NeeddedTweakInfo.Description
  }
}

Because there will be many Tweaks, the usecase of this particular function is for example

//I use a non-existing tweak here for the example
TweakParser("RemoveArrowsFromShortcut","Description")

What I want TweakParser to do now is use RemoveArrowsFromShortcut as the parameter of JSON.parse() but it only accept the name of the JSON variable directly and when I input the name of the first parameter of the TweakParser() function it gives me an error, because the parameter(a variable) itself is not a JSON variable (or whatever it's called like).

So my question to you is:

How can I use the string that the first parameter of TweakParser() contains as a parameter for the JSON.parse() function?

10
  • 2
    so wait, is the TweakName supposed to be your JSON object, like your ShowSecondsInSystemClock var? Commented Aug 27, 2018 at 14:04
  • @basic Yes,it is. Commented Aug 27, 2018 at 14:05
  • Well you are currently passing it as a string, not the actual variable. Have you tried dropping the quotes to pass the actual object? Commented Aug 27, 2018 at 14:06
  • @basic A JSON is a string, that's fine - and it's passed to JSON.parse to create the object Commented Aug 27, 2018 at 14:06
  • Where does RemoveArrowsFromShortcut come from? Commented Aug 27, 2018 at 14:07

1 Answer 1

2

You need to create mapping like a schema 'key': variable example:

{
  'RemoveArrowsFromShortcut': ShowSecondsInSystemClock
}

Full example:

  var ShowSecondsInSystemClock = '{"name":"ShowSecondsInSystemClock","Description":"Patches the System Tray clock to show seconds","ExplorerRestartRequired":"true","category":"UI-Tweaks","badges":"UITweaks"}' 

  var mapping = {
    RemoveArrowsFromShortcut: ShowSecondsInSystemClock 
  };

  function TweakParser(TweakName, NeeddedReturn) {
  
    if (NeeddedReturn == "Description") {
      
      var NeeddedTweakInfo = JSON.parse(mapping[TweakName]); // PAY ATTENTION HERE
      return NeeddedTweakInfo.Description
    }
  }

  var result = TweakParser("RemoveArrowsFromShortcut","Description")

 console.log('result', result)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.