0

Im trying to use a command for a chat program and i want to edit a variable with a command like !editvar variablehere value so if it variablehere = '123' i want to turn '123' into just 123 or something like 'hello' into hello, in simple words im trying to make a string from a chat message into a variable name

ive tried parsing and string.raw and none worked

if(message.startsWith('keyeditvar')) {
        console.log(message)
                var bananasplit = message.split(' ');
                var bananasplitted = json.parse(bananasplit)
                console.log(bananasplitted[0].keyeditvar)
            var variable = bananasplit[1]
            console.log(variable)
            var value = bananasplit[2]
            console.log(value)
            var variable2 = String.raw(variable)
            console.log(variable2)
            var value2 = String.raw(value)
            console.log(value2)
        }

i expected it to work ig

  • 1
    It is hard to understand what you are trying to accomplish. Can you back up and describe what problem you're trying to solve (and not how you're trying to solve it). We need to understand the base problem you're trying to solve and then we can recommend possible solutions. – jfriend00 Aug 22 '19 at 21:49
  • 1
    If you're trying to dynamically refer to variables, just use a map/JavaScript object. Make the variable names keys, then just do a lookup. – Carcigenicate Aug 22 '19 at 21:52
  • 1
    Possible duplicate of ["Variable" variables in Javascript?](https://stackoverflow.com/questions/5187530/variable-variables-in-javascript) – Carcigenicate Aug 22 '19 at 21:53
  • so im trying to do a command to change a variable, like i type !varchange examplevar examplevalue and it will change the variable, im trying to turn a string into a variable name – wreklessbobo Aug 22 '19 at 21:54
  • use an object and store your "variables" as properties of that object. – Thomas Aug 23 '19 at 06:48

2 Answers2

1

im trying to turn a string into a variable name

In Javascript, you don't usually dynamically define new variables with custom names in the current scope. While you can do it at the global scope, you cannot easily do it in the function or block scope without using tools that are generally not recommended (like eval()).

Instead, you use an object and you create properties on that object. You can use either a regular object and regular properties or you can use a Map object with some additional features.

For a regular object, you can do thing like this:

// define base object
let base = {};

// define two variables that contain variable name and value
let someName = "greeting";
let someValue = "hello";

// store those as a property on our base object
base[someName] = someValue;
console.log(base);       // {greeting: "hello"}

Then, you can change the value:

someValue = "goodbye";
base[someName] = someValue;
console.log(base);       // {greeting: "goodbye"}

Or, you can add another one:

let someOtherName = "salutation";
let someOtherValue = "Dear Mr. Green";

base[someOtherName] = someOtherValue;
console.log(base);             // {greeting: "goodbye", salutation: "Dear Mr. Green"}
console.log(base.greeting)     // "goodbye"
console.log(base[someName]);   // "goodbye"
console.log(base.salutation)   // "Dear Mr. Green"
console.log(Object.keys(base)) // ["greeting", "salutation"]

You can think of the Javascript object as a set of key/value pairs. The "key" is the property name and the value is the value. You can get an array of the keys with:

Object.keys(obj)

You set a key/value pair with:

obj[key] = value;

You get the value for a key with:

console.log(obj[key]);

You remove a key/value pair with:

delete obj[key]

With a plain Javascript object like this, the keys must all be strings (or easily converted to a string).

If you have non-string keys, you can use a Map object as it will take any object or primitive value as a key, but it uses get() and set() methods to set and get key/values rather than the assignment scheme of a plain object.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

Please, next time put a clean code...

To convert a string to a number. You can use the function : Number(object)

So for example :

Number('123')

will return 123.

If the object value is not a correct number, it will return NaN.

So for example :

Number('hello')

will return NaN.