Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7585383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:09:45+00:00 2026-05-30T19:09:45+00:00

Can you think of a better way to remap this JavaScript object: { id:

  • 0

Can you think of a better way to remap this JavaScript object:

{
    "id": 123,
    "child.id": 456,
    "child.child.id": 789
}

… into this JavaScript object:

{
    "id": 123,
    "child": {
        "id": 456,
        "child": {
            "id": 789
        }
    }
}

There must be a shorter or faster solution. This is my attempt:

var original = {
    "id": 123,
    "child.id": 456,
    "child.child.id": 789
};

var result = {};

Object.keys(original).forEach(function(key) {
    var node = result;
    var keys = key.match(/(\w+)/g);

    for (var i = 0; i < keys.length; i++) {
        if (!node[keys[i]]) {
            node[keys[i]] = {};
        }
        if (i == keys.length - 1) {
            node[keys[i]] = original[key];
        } else {
            node = node[keys[i]];
        }
    }
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:09:47+00:00Added an answer on May 30, 2026 at 7:09 pm

    I went with your original approach, but used recursion and it seems to be slightly faster in the broswers I’ve tested (except for safari and opera and they are exactly equal, not sure why that is..):

    function remapObject(original){
        var result = {};
        Object.keys(original).forEach(function(key) {
            function splitNode(node, nodes){
                var name = nodes.shift()
                node[name] = (nodes.length >= 1)
                    ? splitNode(node[name] || {}, nodes)
                    : original[key];
                return node;
            }
            splitNode(result, key.split("."));
        });
        return result;
    }
    
    var original = {
            "id": 123,
            "child.id": 456,
            "child.other": 789
        },
        result = remapObject(original)
    
    console.log(original);
    console.log(result);
    

    fiddle is here: http://jsfiddle.net/hyperthalamus/3A9NQ/
    jsperf test is here: http://jsperf.com/mapping-by-keys

    EDIT:
    I had to do something very similar to this recently (again) after spending a lot of time working with closures and optimization, etc. Below is a version that doesn’t rely on Object.keys or array.forEach. array.forEach is generally not good for performance and both of these methods have limited browser support. The function below has been optimized for reuse as this function is used quite a bit in my app’s initialization. I am posting this here (3 months later) because it suited my needs particularly well and I thought I’d share even though this is probably overkill.

    var remapObject = (function () {
        "use strict";
        var nodeSplit = function (key, original) {
            return function splitNode(node, nodes) {
                var name = nodes.shift();
                node[name] = (nodes.length >= 1) ? splitNode(node[name] || {}, nodes) : original[key];
                return node;
            };
        };
        return function (original) {
            var result = {},
                prop,
                splitNode;
            for (prop in original) {
                if (original.hasOwnProperty(prop)) {
                    nodeSplit(prop, original)(result, prop.split("."));
                }
            }
            return result;
        };
    }());
    

    The function at the top is created once inside the initialization enclosure and returns functions to avoid creating a function inside a for loop as this is generally frowned on for optimization or so I’m told.

    If the concern is file size, an compressed version is here:

    var remapObject=function(){var h=function(a,b){return function g(d,e){var f=e.shift();d[f]=1<=e.length?g(d[f]||{},e):b[a];return d}};return function(a){var b={},c;for(c in a)a.hasOwnProperty(c)&&h(c,a)(b,c.split("."));return b}}();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can anyone think of a better way to do this? [AcceptVerbs(HttpVerbs.Post)] public ActionResult SaveAction()
This is probably trivial, but I can't think of a better way to do
The best way I can think of to ask this is by example... In
Is there a better way to write this code without using goto ? It
Apologies in advance for the question title, I can't think of a better way
(Apologies, I can't think of a better way of explaining except through including the
I apologize for the vague title, but I can't think of a better way
OK, that title is a little unclear, but I can't think of a better
I can think of a few messy ways to solve this, but it strikes
I can think of lots of ways to do this on my own, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.