Splitting points into separate javascript objects

I have an object like this:

var data = {"prop.health": 1, "prop.cost":1, "prop.time":1}

I want to change it to an object like this:

{
  "prop": {
    "health": 1, 
    "cost":1, 
    "time":1
  }
}

Here is my code:

  _.each(data, function (value, key) {
    var split = key.split('.')
    if (split.length > 1) {
      data[split[0]] = data[split[0]] || {}
      data[split[0]][split[1]] = value
      delete data[key]
    }
  })

But this only works for level 1 nesting. How could you write it to make sure that it works with as deeply nested properties as you need?

+4
source share
3 answers

You can use a combination _.transformand _.setfor example

data = _.transform(data, function(transformed, val, key) {
    _.set(transformed, key, val);
});

Results in

{"prop":{"health":1,"cost":1,"time":1}}
+4
source

Without a library, it would be something like this:

(function(){
    var data = {"prop.health": 1, "prop.cost":1, "prop.time":1, "prop.test.fun" : 1, "prop.test.sun" : 1};
    var obj = {};  //will hold the object all parsed out
    Object.keys(data).forEach( function (key) {  //loop through the keys in the object
        var val = data[key];  //grab the value of this key
        var step = obj;  //reference the object that holds the values
        key.split(".").forEach(function(part, index, arr){   //split the parts and loop
            if(index===arr.length-1){  //If we are at the last index, than we set the value
                step[part] = val;
            } else if(step[part]===undefined) {  //If we have not seen this key before, create an object
                step[part] = {};
            }
            step = step[part];  //Step up the object we are referencing 
        });
    } );
    console.log(obj);
}());
Run codeHide result

Or a double reduction cycle

(function(){
    var data = {"prop.health": 1, "prop.cost":1, "prop.time":1, "prop.test.fun" : 1, "prop.test.sun" : 1};
    var result = Object.keys(data).reduce( function (obj, key) {  //loop through the keys in the object
        var val = data[key];  //grab the value of this key
        key.split(".").reduce(function(step, part, index, arr){   //split the parts and loop
            if(index===arr.length-1){  //If we are at the last index, than we set the value
                step[part] = val;
            } else if(step[part]===undefined) {  //If we have not seen this key before, create an object
                step[part] = {};
            }
            return step[part];  //Step up the object we are referencing
        }, obj);
        return obj;
    }, {});
    console.log(result);
}());
Run codeHide result
+1
source

(, , , ..), _.set:

var data = {"prop.health": 1, "prop.cost":1, "prop.time":1};

_.each(data, function (value, key) {
  delete data[key];
  _.set(data, key, value);
});

_.set , . :

{"prop":{"health":1,"cost":1,"time":1}}

{"prop.health": 1, "prop.cost.food":1, "prop.time":1} :

{"prop":{"health":1,"cost":{"food":1},"time":1}}
0

Source: https://habr.com/ru/post/1615504/


All Articles