Skip to content

Instantly share code, notes, and snippets.

@jsparmani
Created October 3, 2020 17:32
Show Gist options
  • Save jsparmani/fa18e11b3fb44be59bb81f5b9fd03339 to your computer and use it in GitHub Desktop.
Save jsparmani/fa18e11b3fb44be59bb81f5b9fd03339 to your computer and use it in GitHub Desktop.
initial = {
user_name: "Jay",
user_details_lastname: "Parmani",
user_details_random: "Hello",
user_details_address_city: "Surat",
};
final = {};
for (var key in initial) {
if (initial.hasOwnProperty(key)) {
partitioned = String(key).split("_");
temp = final;
partitioned.forEach((element, i) => {
if (!temp.hasOwnProperty(element)) {
temp[element] = {};
}
temp = temp[element];
});
temp[partitioned[partitioned.length - 1]] = initial[key];
}
}
console.log(final);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment