1

I have a function defined that way:

var myObject = new Object();

    function myFunction(someString){

         myObject.someString= 0;

    }

The problem is someString is taken as the string someString instead of the value of that variable.

So, after I use that function several times over with different someString's, I would like to get an object with all the values for each someString.

But when I loop through that object The only result I get is someString : 0

I want to get:

John : 0
James : 0
Alex : 0 etc....

How do I do that? Thanks a lot in advance

2 Answers 2

4

You can use the associative array approach:

var someString = 'asdf';

myObject[someString] = 0; // {asdf: 0}
myObject['foo'] = 'bar';

You can basically use any string to retrieve your method/parameter.

var i = 1;
myObject['test' + i] = i + 1; // gives us {test1: 2}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

var myObject = new Object();
    function myFunction(someString){
        myObject[someString]= 0;
    }

Comments

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.