4

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'
    }
}
3
  • Not exactly the same, but maybe "Create nested Object based on String Properties" helps. Commented Mar 1, 2018 at 10:43
  • You probably need some recursion in there Commented Mar 1, 2018 at 10:44
  • 'app.team.instance.obj' is not valid path. Commented Mar 1, 2018 at 10:46

2 Answers 2

4

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);

Sign up to request clarification or add additional context in comments.

Comments

3

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; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.