How to use a variable as a function parameter in Javascript?

How to use a variable as a function parameter in Javascript?



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).


TweakParser


RemoveArrowsFromShortcut


JSON.parse()


TweakParser()



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?


TweakParser()


JSON.parse()





so wait, is the TweakName supposed to be your JSON object, like your ShowSecondsInSystemClock var?
– basic
Aug 27 at 14:04





@basic Yes,it is.
– libert1quinten
Aug 27 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?
– basic
Aug 27 at 14:06





@basic A JSON is a string, that's fine - and it's passed to JSON.parse to create the object
– Bergi
Aug 27 at 14:06


JSON.parse





Where does RemoveArrowsFromShortcut come from?
– Bergi
Aug 27 at 14:07


RemoveArrowsFromShortcut




1 Answer
1



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


'key': variable



'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)





@alexdykyi This works like a charm,thank you!
– libert1quinten
Aug 27 at 14:18






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế