26

I'm trying to build a function that would expand an object like :

{
    'ab.cd.e' : 'foo',
    'ab.cd.f' : 'bar',
    'ab.g' : 'foo2'
}

Into a nested object :

{ab: {cd: {e:'foo', f:'bar'}, g:'foo2'}}

Like this php function : Set::expand()

Without using eval of course.

Kara
  • 5,996
  • 16
  • 49
  • 56
Olivier
  • 3,363
  • 5
  • 34
  • 55
  • 1
    related: [Fastest way to flatten / un-flatten nested JSON objects](http://stackoverflow.com/q/19098797/1048572) – Bergi Dec 09 '15 at 22:09
  • If it should also work for nested objects, see here: https://silvantroxler.ch/2018/object-string-property-nesting/ – str Nov 14 '18 at 14:00

7 Answers7

41

I believe this is what you're after:

function deepen(obj) {
  const result = {};

  // For each object path (property key) in the object
  for (const objectPath in obj) {
    // Split path into component parts
    const parts = objectPath.split('.');

    // Create sub-objects along path as needed
    let target = result;
    while (parts.length > 1) {
      const part = parts.shift();
      target = target[part] = target[part] || {};
    }

    // Set value at end of path
    target[parts[0]] = obj[objectPath]
  }

  return result;
}

// For example ...
console.log(deepen({
  'ab.cd.e': 'foo',
  'ab.cd.f': 'bar',
  'ab.g': 'foo2'
}));
broofa
  • 36,284
  • 11
  • 69
  • 73
  • 3
    I just put a more comprehensive solution down below: https://github.com/Gigzolo/dataobject-parser – Henry Tseng Mar 06 '14 at 19:37
  • This is risky if you have a same-name top level property. Wrap from `t=oo;` to `t[key] = o[k]` in `if (k.indexOf('.') !== -1)` ... – brandonscript Apr 14 '14 at 19:14
  • It also doesn't work if you have more than one top-level key. – brandonscript Apr 14 '14 at 19:15
  • @brandonscript I believe this works fine with multiple top-level properties. I tested with the `pizza`/`this.other`/`this.thing.that` example in your answer and get the same result as you. – broofa Jul 27 '20 at 12:55
9

If you're using Node.js (e.g. - if not cut and paste out of our module), try this package: https://www.npmjs.org/package/dataobject-parser

Built a module that does the forward/reverse operations:

https://github.com/Gigzolo/dataobject-parser

It's designed as a self managed object right now. Used by instantiating an instance of DataObjectParser.

var structured = DataObjectParser.transpose({
    'ab.cd.e' : 'foo',
    'ab.cd.f' : 'bar',
    'ab.g' : 'foo2'
});                                                                                                                                                                                                                                                                                                                                                                                                                                                   

structured.data() returns your nested object:

{ab: {cd: {e:'foo', f:'bar'}, g:'foo2'}}

So here's a working example in JSFiddle:

http://jsfiddle.net/H8Cqx/

Henry Tseng
  • 2,953
  • 1
  • 17
  • 17
5

Function name is terrible and the code was quickly made, but it should work. Note that this modifies the original object, I am not sure if you wanted to create a new object that is expanded version of the old one.

(function(){

    function parseDotNotation( str, val, obj ){
    var currentObj = obj,
        keys = str.split("."), i, l = keys.length - 1, key;

        for( i = 0; i < l; ++i ) {
        key = keys[i];
        currentObj[key] = currentObj[key] || {};
        currentObj = currentObj[key];
        }

    currentObj[keys[i]] = val;
    delete obj[str];
    }

    Object.expand = function( obj ) {

        for( var key in obj ) {
        parseDotNotation( key, obj[key], obj );
        }
    return obj;
    };

})();



var expanded = Object.expand({
    'ab.cd.e' : 'foo',
        'ab.cd.f' : 'bar',
    'ab.g' : 'foo2'
});



JSON.stringify( expanded );  


//"{"ab":{"cd":{"e":"foo","f":"bar"},"g":"foo2"}}"
Esailija
  • 134,577
  • 23
  • 263
  • 318
  • This is great, but only works if there is a single top-level key. If you had `{"foo":"bar", "foo.baz":"bar", "baz":"bar"}` it'll drop the "baz" key. – brandonscript Apr 14 '14 at 19:34
3

Derived from Esailija's answer, with fixes to support multiple top-level keys.

(function () {
    function parseDotNotation(str, val, obj) {
        var currentObj = obj,
            keys = str.split("."),
            i, l = Math.max(1, keys.length - 1),
            key;

        for (i = 0; i < l; ++i) {
            key = keys[i];
            currentObj[key] = currentObj[key] || {};
            currentObj = currentObj[key];
        }

        currentObj[keys[i]] = val;
        delete obj[str];
    }

    Object.expand = function (obj) {
        for (var key in obj) {
            if (key.indexOf(".") !== -1)
            {
                parseDotNotation(key, obj[key], obj);
            }            
        }
        return obj;
    };

})();

var obj = {
    "pizza": "that",
    "this.other": "that",
    "alphabets": [1, 2, 3, 4],
    "this.thing.that": "this"
}

Outputs:

{
    "pizza": "that",
    "alphabets": [
        1,
        2,
        3,
        4
    ],
    "this": {
        "other": "that",
        "thing": {
            "that": "this"
        }
    }
}

Fiddle

Community
  • 1
  • 1
brandonscript
  • 62,971
  • 29
  • 152
  • 211
3

You could split the key string as path and reduce it for assigning the value by using a default object for unvisited levels.

function setValue(object, path, value) {
    var keys = path.split('.'),
        last = keys.pop();

    keys.reduce((o, k) => o[k] = o[k] || {}, object)[last] = value;
    return object;
}

var source = { 'ab.cd.e': 'foo', 'ab.cd.f': 'bar', 'ab.g': 'foo2' },
    target = Object
        .entries(source)
        .reduce((o, [k, v]) => setValue(o, k, v), {});

console.log(target);
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358
  • what if we have a key repeated twice? looks like it is keeping the last value only... – user3174311 Oct 21 '19 at 09:26
  • @user3174311, right. what would you like to get instead? – Nina Scholz Oct 21 '19 at 09:33
  • in case a key is repeated I would like to get an array or object. thank you. – user3174311 Oct 21 '19 at 09:38
  • @user3174311, with this special case, you could check if the property has a value and change the type to array with the first value. but this needs to check if a value is already given, too. maybe you ask a new question with this problem and add what you have tried, as well. – Nina Scholz Oct 21 '19 at 09:57
  • I asked it here: https://stackoverflow.com/questions/58482952/how-to-split-a-string-into-an-associative-array-in-js I am going to add some come I tried. Thank you. – user3174311 Oct 21 '19 at 10:01
  • sorry, i hadn't read the question in depth. what you want is a special case. – Nina Scholz Oct 21 '19 at 10:03
  • how can I change this to only returns array? even when we have a single element? – user3174311 Oct 21 '19 at 11:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201200/discussion-between-nina-scholz-and-user3174311). – Nina Scholz Oct 21 '19 at 11:16
0

You need to convert each string key into object. Using following function you can get desire result.

 function convertIntoJSON(obj) {

                var o = {}, j, d;
                for (var m in obj) {
                    d = m.split(".");
                var startOfObj = o;
                for (j = 0; j < d.length  ; j += 1) {

                    if (j == d.length - 1) {
                        startOfObj[d[j]] = obj[m];
                    }
                    else {
                        startOfObj[d[j]] = startOfObj[d[j]] || {};
                        startOfObj = startOfObj[d[j]];
                    }
                }
            }
            return o;
        }

Now call this function

 var aa = {
                'ab.cd.e': 'foo',
                'ab.cd.f': 'bar',
                    'ab.g': 'foo2'
                };
   var desiredObj =  convertIntoJSON(aa);
Anoop
  • 22,496
  • 10
  • 60
  • 70
0

Something that works, but is probably not the most efficient way to do so (also relies on ECMA 5 Object.keys() method, but that can be easily replaced.

var input = {
    'ab.cd.e': 'foo',
    'ab.cd.f': 'bar',
    'ab.g': 'foo2'
};

function createObjects(parent, chainArray, value) {
    if (chainArray.length == 1) {
        parent[chainArray[0]] = value;
        return parent;
    }
    else {
        parent[chainArray[0]] = parent[chainArray[0]] || {};
        return createObjects(parent[chainArray[0]], chainArray.slice(1, chainArray.length), value);
    }
}

var keys = Object.keys(input);
var result = {};

for(var i = 0, l = keys.length; i < l; i++)
{
    createObjects(result, keys[i].split('.'), input[keys[i]]);
}

JSFiddle is here.

ZenMaster
  • 11,513
  • 5
  • 34
  • 57