Q:

How to map the values of all the leaf nodes in Javascript? 

I have a nested JSON object tree like this :
{"children":[{"name":"Afghanistan","children":[{"World 
Cities. Country":"Afghanistan","World Cities. Population":2997,"World 
Cities. City":"Qal eh-ye Now"},{"World Cities. Country":"Afghanistan","World 
Cities. Population":7407,"World Cities. City":"Mahmud-E Eraqi"},{"World 
Cities. Country":"Afghanistan","World Cities. Population":10000,"World 
Cities. City":"Tarin Kowt"}]

EXPECTED OUTPUT IS SOMETHING LIKE THIS
{
"name": "flare",
  "children": [
    {
      "name": "LEGISLATIVE",
      "children": [
        {
          "name": "Legislative Branch",
          "size": 20891
        }
      ]
    },
    {
      "name": "JUDICIAL",
      "children": [
        {
          "name": "Supreme Court Law Library",
          "size": 1589.3
        },
        {
          "name": "New Mexico Compilation Commission",
          "size": 1853.5
        },
        {
          "name": "Judicial Standards Commission",
          "size": 860.8
        },
        {
          "name": "Court of Appeals",
          "size": 5960.2
        },
        {
          "name": "Supreme Court",
          "size": 3421.1
        },
        {
          "name": "Supreme Court Building Commission",
          "size": 977.4
        },
        {
          "name": "Administrative Office of the Courts",
          "size": 65870.4
        },
        {
          "name": "First Judicial District Court",
          "size": 8372.5
        },
        {
          "name": "Second Judicial District Court",
          "size": 28154.6
        },
        {
          "name": "Third Judicial District Court",
          "size": 7808.3
        },
        {
          "name": "Fourth Judicial District Court",
          "size": 2577.2
        },
        {
          "name": "Fifth Judicial District Court",
          "size": 7389.5
        },
        {
          "name": "Sixth Judicial District Court",
          "size": 3630.7
        },
        {
          "name": "Seventh Judicial District Court",
          "size": 2895
        },

Expected output for my example :
{"children":[{"name":"Afghanistan","children":[{"name":"Qal eh-ye Now","value":2997}]]}

So I want to map the values of the leaf nodes.   In this example  ,World Cities. Country,World Cities. Population and World Cities .City to just a name and value for every children object. 
Currently this is the function that I have :
var mappedData = nestedData.children.map(function (d) {
                      return { name: d[groupKey], value: d[sizeKey] };
                  }); //groupKey is World. Cities. City ,sizeKey is Population

But obviously this syntax does not work because I first need to access all the leaf nodes of this tree then apply the map function to it.  How can I achieve that?  

A:

This is how you can achieve that:

    const a = {
    children: [
        {
            name: "Afghanistan",
            children: [
                {
                    "World Cities. Country": "Afghanistan",
                    "World Cities. Population": 2997,
                    "World Cities. City": "Qal eh-ye Now"
                },
                {
                    "World Cities. Country": "Afghanistan",
                    "World Cities. Population": 7407,
                    "World Cities. City": "Mahmud-E Eraqi"
                },
                {
                    "World Cities. Country": "Afghanistan",
                    "World Cities. Population": 10000,
                    "World Cities. City": "Tarin Kowt"
                }
            ]
        }
    ]
};

const deep = x => {
    if (Array.isArray(x.children)) {
        x.children = x.children.map(deep);
        return x;
    } else {
        return {
            name: x["World Cities. City"],
            value: x["World Cities. Population"]
        };
    }
};

const b = deep(a);

console.log(b);

