Junius L
Junius L

Reputation: 16122

How to turn dot notation string into an object in Javascript

I'm trying to turn a dot notation string into an object, for example given

  [{
      key: 'app.team.instance',
      value: 'some value1'
  }, {
      key: 'app.team.instance.obj',
      value: 'some value'
  }, {
      key: 'app.team.app.some',
      value: 'some value'
  }, {
      key: 'app.service.awesome.more',
      value: 'more values'
  }]

I would like to turn it an object like to get

 {
  "team": {
      "instance": "some value1",
      "server": {
          "obj": "some value"
      },
      "app": {
          "some": "some value"
      }
  },
  "service": {
      "awesome": {
          "more": "more values"
      }
  }
}

This's what I have tried using the following function. Looking at my function what have I missed or should improve?

function createObjFromRows(skip, key, value, obj) {
    const ARRAY_KEYS = key.split('.');
    const ARRAY_LENGTH = ARRAY_KEYS.length;

    let i = skip ? 1 : 0;

    for (; i < ARRAY_LENGTH; i++) {
        if (i < (ARRAY_LENGTH - 1)) {
            if (!obj.hasOwnProperty(ARRAY_KEYS[i])) { obj[ARRAY_KEYS[i]] = {}; }
        } else {
            obj[ARRAY_KEYS[i - 1]][ARRAY_KEYS[i]] = value;
        }
    }
}

This's what I get currently.

{
    team: {
        instance: 'some value1'
    },
    server: {
        obj: 'some value'
    },
    app: {
        some: 'some value'
    },
    service: {},
    awesome: {
        more: 'more values'
    }
}

Upvotes: 4

Views: 3589

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386560

You could split the given key strings and save the last key for the assignment of the value after iterating the keys to the nested property.

var data = [{ key: 'app.team.instance', value: 'some value1' }, { key: 'app.team.server.obj', value: 'some value' }, { key: 'app.team.app.some', value: 'some value' }, { key: 'app.service.awesome.more', value: 'more values' }],
    result = data.reduce(function (r, o) {
        var path = o.key.split('.'),
            last = path.pop();

        path.reduce(function (p, k) {
            return p[k] = p[k] || {};
        }, r)[last] = o.value;
        return r;
    }, {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 3

Faly
Faly

Reputation: 13346

You can use array.prototype.reduce :

  var datas = [ {key: 'app.team.instance', value: 'some value1'}, {key: 'app.team.server.obj', value: 'some value'}, {key: 'app.team.app.some',value: 'some value'}, {key: 'app.service.awesome.more', value: 'more values'}];

var res = datas.reduce((m, o) => {
    var keys = o.key.split('.');
    var cur = m;
    keys.forEach((key, i) => { 
        if (i < keys.length - 1) {
            cur[key] = cur[key] || {};
            cur = cur[key];
        } else {
            cur[key] = o.value;
        }
    });
    return m;
}, {});

console.log(res);

Upvotes: 4

Related Questions