Convert String in Dot Notation to Get the Object Reference

Convert string in dot notation to get the object reference

To obtain the value, consider:

function ref(obj, str) {
str = str.split(".");
for (var i = 0; i < str.length; i++)
obj = obj[str[i]];
return obj;
}

var obj = { a: { b: 1, c : { d : 3, e : 4}, f: 5 } }
str = 'a.c.d'
ref(obj, str) // 3

or in a more fancy way, using reduce:

function ref(obj, str) {
return str.split(".").reduce(function(o, x) { return o[x] }, obj);
}

Returning an assignable reference to an object member is not possible in javascript, you'll have to use a function like the following:

function set(obj, str, val) {
str = str.split(".");
while (str.length > 1)
obj = obj[str.shift()];
return obj[str.shift()] = val;
}

var obj = { a: { b: 1, c : { d : 3, e : 4}, f: 5 } }
str = 'a.c.d'
set(obj, str, 99)
console.log(obj.a.c.d) // 99

or use ref given above to obtain the reference to the containing object and then apply the [] operator to it:

parts = str.split(/\.(?=[^.]+$)/)  // Split "foo.bar.baz" into ["foo.bar", "baz"]
ref(obj, parts[0])[parts[1]] = 99

Convert dot notation string to object and lookup reference object

With your

x = x[s[i]] = {}; 

You're unconditionally overwriting the s[i] property even if it's already been populated. Only assign a new object if another object doesn't already exist at that property.

if (x[s[i]]) {
x = x[s[i]];
} else {
x = x[s[i]] = {};
}

function convert(base,reference) {    var obj = {};
var getObjectValue = function(string, reference) { var i = 0; while (reference && i < string.length) { reference = reference[string[i++]]; } return reference; };
for (var n = 0; n < base.length; n++) { var s = base[n].split("."), x = obj;
for(var i = 0; i < s.length-1; i++) { if (x[s[i]]) { x = x[s[i]]; } else { x = x[s[i]] = {}; } }
x[s[i]] = getObjectValue(s,reference); }
return obj;
}
var base = ['a.b.c','a.e','h'];
var reference = { a: { b: { c:"Hello", d:"you" }, e:"beautiful", f:"and" }, g:"kind", h:"World" };
var desiredOutput = { a: { b: { c:"Hello" }, e:"beautiful" }, h:"World" };

console.log(convert(base,reference));

How to turn dot notation string into an object in Javascript

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

How transform string dot notation to nested object?

You can split the string and use the parts for the reference with Array.prototype.reduce(), where obj is used as the start object and while iterating, the new reference is returned.

var str = 'a.b.c.d',    obj = {};
str.split('.').reduce(function (r, a) { r[a] = r[a] || {}; return r[a];}, obj);
document.write('<pre>' + JSON.stringify(obj, 0, 4) + '</pre>');


Related Topics



Leave a reply



Submit