-1

I have a json object:

var config = 
{
    "data1" :
    {
        "emp1":
        {
            "phone" : "111",
            "address": "xxx"
        },
        "emp2":
        {
            "phone" : "111",
            "address": "xxx"
        }
    }   
}

In my function I pass the root object and the string of the name I want:

function getEmp(config, section)
{
}

config is the json object above. Section is a string for either emp1 or emp2. I would like to be able to combine for example use the function like so

var emp1Data = getEmp(config, 'emp1')

What is the best way to return that portion of the JSON object?

Ive tried:

function getEmp(config, section)
{
  return JSON.parse(config + '.' + emp);
}

but this fails. I would like to be able to do it without looping in the function.

Update 1 My config object has an extra comma. I have updated it

Update 2 I fixed my config object now

adviner
  • 3,295
  • 10
  • 35
  • 64

5 Answers5

0

If you have a Javascript Object:

var config = {
    "emp1":
    {
        "phone" : "111",
        "address": "xxx"
    },
    "emp2":
    {
        "phone" : "111",
        "address": "xxx"
    }
}

Then you could access it like so:

(function(config, section) {
  return config[section];
})(config, 'emp1');

Using [] notation to access the property of the config object, which is similar to config.emp1.

See the following MDN article on [] property accessor notation: Property Accessors

forTruce
  • 829
  • 2
  • 7
  • 17
0

Your config is poorly defined. You want something like:

var config =
{
    "emp1":
    {
        "phone" : "111",
        "address": "xxx"
    },
    "emp2":
    {
        "phone" : "111",
       "address": "xxx"
    }       
};

Then, to get what you want, you do (for example)

var blah = config["emp1"];
console.log( JSON.stringify( blah ) );
CodeHunter
  • 11
  • 3
0

You can use the for..in loop to traverse through the different property names of the object and its children. See here for an example of what you want:

function getEmp(config, section)
{
    for(var cur in config){
        if(cur == section){
            return config[cur];
        }else if(typeof config[cur] == 'object'){
            var result = getEmp(config[cur], section);
            if(typeof result != 'undefined'){
                return result;
            }
        }
    }
}
Pluto
  • 2,900
  • 27
  • 38
0
function deepGet(root, path) {
    let output = root;
    for (const e of path) output = output[e];
    return output;
}

Example:

function deepGet(root, path) {
 let output = root;
 for (const e of path) output = output[e];
 return output;
}

const a = [];
a.b = [];
a.b.c = 'd';

console.log(deepGet(a, ['b', 'c']) === a.b.c);
console.log(deepGet(a, ['b']) === a.b);
console.log(deepGet(a, []) === a);

Benchmark

Mir-Ismaili
  • 13,974
  • 8
  • 82
  • 100
-1

You can use eval() to do that

function getEmp(config, section)
{
    return eval( "(" + config + "." + section  + ")" )
}

but you will have to make sure config is a string, not an object.

Meaning: you will have to call that function like this:

var sec = getEmp('config','emp2');
Ahmad
  • 12,336
  • 6
  • 48
  • 88