Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to convert a string into a variable and use it as a key to call a value from map?

    Greetings to all! I will go straight to the point.

    I am using node.js engine to write javascript code and debug. I have created a map with some values and I want to use the key_value content passed to the function to call a value from this map.

    Ideally:
    E.g., If key_value= "age"
    return Students["Mark"].age


    Originator.prototype.student= function(originator_name, key_value){ // key_value="age"
    var Students = {
    "Mark": {
    age: 23,
    hair_color: "brown",
    height: 1.78
    },
    "Tom": {
    age: 19,
    hair_color: "black",
    height: 1.74

    },
    "Nick": {
    age: 26,
    hair_color: "blonde",
    height: 1.78
    }
    };

    var operator = Students["Mark"];
    return operator.key_value; // where key value can be age.
    };

    I tried print out the Node.js global function below to convert the string "age" to a variable and use it as a key:

    console.log(Students["Mark"].global[key_value]);

    but I get this error:

    console.log(Students["Mark"].global[key_value]);
    ^
    TypeError: Cannot read property 'age' of undefined.

    Does anybody know how to do this?

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,984
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I don't know what the originator prototype is, but looking at it as a straight function might help:

    Code:
    <script>
    var student= function(originator_name, key_value){ // key_value="age"
    var Students = {
    "Mark": {
    age: 23,
    hair_color: "brown",
    height: 1.78
    },
    "Tom": {
    age: 19,
    hair_color: "black",
    height: 1.74
    
    },
    "Nick": {
    age: 26,
    hair_color: "blonde",
    height: 1.78
    }
    };
    
    var operator = Students[originator_name];
    
    return operator[key_value]; // where key value can be age.
    };
    
    console.log(student("Mark","age"));
    </script>

  • #3
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    I don't know what the originator prototype is, but looking at it as a straight function might help:

    Code:
    <script>
    var student= function(originator_name, key_value){ // key_value="age"
    var Students = {
    "Mark": {
    age: 23,
    hair_color: "brown",
    height: 1.78
    },
    "Tom": {
    age: 19,
    hair_color: "black",
    height: 1.74
    
    },
    "Nick": {
    age: 26,
    hair_color: "blonde",
    height: 1.78
    }
    };
    
    var operator = Students[originator_name];
    
    return operator[key_value]; // where key value can be age.
    };
    
    console.log(student("Mark","age"));
    </script>
    Hello xelawho,
    Thanks a lot for your time replying this. Your suggestion solves the problem I have.

    Can you please explain what is the difference between using these two calls:

    operator[key_value]; // where key_value="age"
    operator.age;

    They both seem to give the same result but the first is a string and the second a variable?
    I was trying to convert the string "age" to a variable age but it seems I didn't need to.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Yperionas View Post
    operator[key_value]; // where key_value="age"
    operator.age;
    They are two different ways to reference the same property. operator.age and operator['age'] are the same property just referenced two different ways.

    As the [] variant uses a string for the property name you can substitute a variable for the string so as to be able to access different properties depending on the value of the string.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    The dot notation and square bracket notation are basically the same. But the latter is more flexible and powerful as you can have any js expression inside the square brackets.
    Code:
    obj[ someVar ]
    obj[ callSomeFunctionThatReturnsSomething(param1, param2) ]
    obj[ "someString" + someVar ] 
    obj[ someObj.someKey ]
    obj[ someArray[i] ]
    obj[ someVar ? "foo" : "bar" ]
    obj[ someVar || "defaultKey" ]
    obj["key-with-dashes"] //this cannot be referenced using dot notation
    obj["key with spaces"] //this cannot be referenced using dot notation
    
    ...and so on
    Last edited by glenngv; 03-05-2014 at 01:36 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •