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

5
  • 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. Commented Aug 22, 2019 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. Commented Aug 22, 2019 at 21:52
  • 1
    Possible duplicate of "Variable" variables in Javascript? Commented Aug 22, 2019 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 Commented Aug 22, 2019 at 21:54
  • use an object and store your "variables" as properties of that object. Commented Aug 23, 2019 at 6:48

2 Answers 2

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.

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

Comments

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.

4 Comments

yeah how can i do that with the hello
"hello" is not a number. How could you convert it into a legal number ? The only think you can do is creating your own algorithm which will convert none number string into number.
im not trying to do just a number im trying to do words too
So in which number you want to convert "hello" ?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.