Handling Parameters of Objects with Multiple Levels in Javascript

1

I am creating a service within an angular application where I use a Value to generate a kind of session. thinking about the scenario:

angular.module("app.values", [])
    .value("a", {
        b : {
            c : {
                d: "joao"
            }
        }
    })
;

Thinking about the operation of a simple session basically I would have 2 functions: get , set . So far I've been able to think of the get type of function that looks like this:

function get(object, param) {
    var level = param.split("."), value = object;
    for (var i in level) 
      value = value[level[i]];
    return value;
}

The problem is that I can not think of a method to do the set function to manipulate only the specific value of an object, I've thought about it so far ...

function(object, param, value){
    var level = param.split("."), object = object, value = value;
    for(var i = 0; i < level.length; i++){
        //e agora?????
    }
}
    
asked by anonymous 01.04.2016 / 17:40

1 answer

1
function set(object, param, value) {
  var levels = param.split('.');
  var lastLevel = levels.pop();
  get(object, levels.join('.'))[lastLevel] = value;
}
    
01.04.2016 / 18:28