0

I am new to Nodejs and typescript.I have below two jsons :

json1 = 
{
    "201809": 2,
    "metric": "headcount",
    "quarter1": 60,
    "careerLevelsGroups": [{
        "201809": 2,
        "quarter1": 60,
        "careerLevels": [{
                "201809": 2,
                "careerId": "careerId1",
                "quarter1": 60,
            },
            {
                "201809": 2,
                "careerId": "careerId2",
                "quarter1": 50,
            }
        ]
    }]
}

json2 = 
    {
        "201809": 3,
        "metric": "headcount",
        "quarter1": 100,
        "careerLevelsGroups": [{
            "201809": 7,
            "quarter1": 40,
            "careerLevels": [{
                    "201809": 9,
                    "careerId": "careerId1",
                    "quarter1": 30,
                },
                {
                    "201809": 8,
                    "careerId": "careerId2",
                    "quarter1": 30,
                }
            ]
        }]
    }

I want to sum all the numeric values associated with the same keys and produce a single json having the summed values of json1 and json2.

result = 
{
        "201809": 5,
        "metric": "headcount",
        "quarter1": 160,
        "careerLevelsGroups": [{
            "201809": 9,
            "quarter1": 100,
            "careerLevels": [{
                    "201809": 11,
                    "careerId": "careerId1",
                    "quarter1": 90,
                },
                {
                    "201809": 10,
                    "careerId": "careerId2",
                    "quarter1": 80,
                }
            ]
        }]
    }

I am trying to use a loop but i have too many such elements which i need to sum so can you please suggest a more optimised method to be used in Node.js

5
  • 3
    please include attempted script and expected output Commented Apr 24, 2018 at 21:42
  • i have added the expected result. i have not attempted any script yet as i am not able to get any other idea except use a loop. I don't want to use loop as i have 100s of these elements to be added. Commented Apr 24, 2018 at 21:49
  • I'd use a loop. I'd show some attempt to solve the problem too, you'll get an answer. Commented Apr 24, 2018 at 22:01
  • 1
    i recommend recursion. think of processing all the scalar members in the function and processing the object members as a reentrant call to the same function. i cannot easily type as im using my smartphone. hopefully this hint will help u Commented Apr 24, 2018 at 22:05
  • 1
    FYI, those are two javascript objects, not JSON Commented Apr 24, 2018 at 22:53

1 Answer 1

1

I put together a combine() function which uses recursion for processing members of the JSON object. If it detects a "number" it combines it. If it detects an "object" it applies recursion to search for more "number" or "object" inside it. For any other type (e.g. "string") it just leaves the values as is.

function combine(acc, curr) {
  for (var key in acc) {
    switch (typeof(acc[key])) {
    case "number":
      acc[key] += curr[key];
      break;
    case "object":
      combine(acc[key], curr[key]);
      break;
    }
  }
}

var json1 = 
{
    "201809": 2,
    "metric": "headcount",
    "quarter1": 60,
    "careerLevelsGroups": [{
        "201809": 2,
        "quarter1": 60,
        "careerLevels": [{
                "201809": 2,
                "careerId": "careerId1",
                "quarter1": 60,
            },
            {
                "201809": 2,
                "careerId": "careerId2",
                "quarter1": 50,
            }
        ]
    }]
};

var json2 = 
    {
        "201809": 3,
        "metric": "headcount",
        "quarter1": 100,
        "careerLevelsGroups": [{
            "201809": 7,
            "quarter1": 40,
            "careerLevels": [{
                    "201809": 9,
                    "careerId": "careerId1",
                    "quarter1": 30,
                },
                {
                    "201809": 8,
                    "careerId": "careerId2",
                    "quarter1": 30,
                }
            ]
        }]
    };
    
var res = json1;
combine(res, json2);
console.log(JSON.stringify(json1, undefined, 2));

Running the script produces the desired output. It should be a straightforward execise, to iterate through a series of these JSON objects to generate the result required.

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

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.