javascript - How does one parse an object with points (.) included in the keys to an multi leveled object? -
javascript - How does one parse an object with points (.) included in the keys to an multi leveled object? -
this question has reply here:
convert javascript string in dot notation object reference 14 answershow 1 parse object points (.) included in keys multi leveled object?
example:
{ "data.firstname": "john", "data.lastname": "doe", "data.email": "example@example.org" }
expected result:
{ data: { firstname: "john", lastname: "doe", email: "example@example.org" } }
ps: can find collections in mongodb's find query, couldn't find how it, hence question.
you like:
var info = { "data.firstname": "john", "data.lastname": "doe", "data.email": "example@example.org" } var result = {}; object.keys(data).foreach(function (key) { var value = data[key]; var keyparent = key.split(".")[0]; var keychild = key.split(".")[1]; if(!result[keyparent]){ result[keyparent] = {}; } result[keyparent][keychild] = value; });
and result object results looking for.
javascript mongodb
Comments
Post a Comment