'How do construct id/parentIds with a recursive function

I am trying to build data for a devexpress tree grid using a id/parentId relationship. The id needs to include the complete parent path which will be the parentId, that parentId, that parentId, etc...

So if you look at this screenshot. SCREENSHOT

the result I am looking for needs to look like this.

[
  {
    id: "EOG",
    parentId: null,
  },
  {
    id: "EOG~division_name~Midland",
    parentId: "EOG",
  },
  {
    id: "EOG~division_name~Midland~entity_color~Blue",
    parentId: "EOG~division_name~Midland",
  },
  {
    id: "EOG~division_name~Midland~entity_color~Blue~area~Area 2",
    parentId: "EOG~division_name~Midland~entity_color~Blue",
  },
  {
    id: "EOG~division_name~Midland~entity_color~Blue~area~Area 1",
    parentId: "EOG~division_name~Midland~entity_color~Blue",
  },
  {
    id: "EOG~division_name~Midland~entity_color~Blue~area~UNKNOWN",
    parentId: "EOG~division_name~Midland~entity_color~Blue",
  },
];

It is 0-N hierarchy setup. You can see what hierarchy is currently selected

Division / Entity Color / Area

and I need a recursive function to build the ids/parentIds. Here is the JSON from the API.

[
  {
    "id": "EOG",
    "parentId": null
  },
  {
    "id": "CORPUS CHRISTI",
    "level": "1",
    "memSqlColumnName": "division_name",
    "parentId": "EOG",
    "parentMemSqlColumnName": ""
  },
  {
    "id": "CHINA",
    "level": "1",
    "memSqlColumnName": "division_name",
    "parentId": "EOG",
    "parentMemSqlColumnName": ""
  },
  {
    "id": "DENVER",
    "level": "1",
    "memSqlColumnName": "division_name",
    "parentId": "EOG",
    "parentMemSqlColumnName": ""
  },
  {
    "id": "HEADQUARTERS",
    "level": "1",
    "memSqlColumnName": "division_name",
    "parentId": "EOG",
    "parentMemSqlColumnName": ""
  },
  {
    "id": "MIDLAND",
    "level": "1",
    "memSqlColumnName": "division_name",
    "parentId": "EOG",
    "parentMemSqlColumnName": ""
  },
  {
    "id": "BLUE",
    "level": "2",
    "memSqlColumnName": "entity_color",
    "parentId": "MIDLAND",
    "parentMemSqlColumnName": "division_name"
  },
  {
    "id": "AREA 2",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "AREA 1",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "UNKNOWN",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "AREA 7",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "RED HILLS",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "RED HILLS NORTH",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "BETA",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "ZENA WEST",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "UNASSIGNED",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "STATELINE",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "K-BAR CORRIDOR",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "NORTH DJ",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "AREA 5",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "UNASSIGNED",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "BAKKEN LITE",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "UNASSIGNED",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "KBAR",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "NORTH POWDER",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "CORE",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "SOUTH POWDER",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "AREA 9",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "POWDER RIVER",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "SPR OFF-CORRIDOR",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "ANTELOPE",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "PEGASUS",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "SOLD",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "WEST",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "ZENA",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "CORRIDOR A",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "ATHENA",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "0",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "AREA 2",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "ZENA EAST",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "IMPOSSIBLE",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "SDJ",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "AREA 3",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "AREA 3",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "UNASSIGNED",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "AREA 1",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "UNASSIGNED",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "AREA 8",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "SOUTH DJ",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "UNASSIGNED",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "NPR CORRIDOR",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "0",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "HOBBS",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "ALPHA",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "KING RANCH",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "UNASSIGNED",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "UNDAUNTED",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "RED HILLS SOUTH",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "HERCULES",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "ROSS DRAW",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "AREA 1",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "ROSS",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "DELAWARE BASIN",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "WEST",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "UNASSIGNED",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "AREA 1",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "AREA 6",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "UNASSIGNED",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "DELAWARE BASIN",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "CENTRAL",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "UNASSIGNED",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "BAKKEN",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "CONAN",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "AREA 1",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "AREA 1",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "SPR CORRIDOR",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "ELM COULEE",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "CARLSBAD",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "K-BAR",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "AREA 4",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "NPR OFF-CORRIDOR",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "EAST",
    "level": "3",
    "memSqlColumnName": "area",
    "parentId": "BLUE",
    "parentMemSqlColumnName": "entity_color"
  },
  {
    "id": "BLACK",
    "level": "2",
    "memSqlColumnName": "entity_color",
    "parentId": "MIDLAND",
    "parentMemSqlColumnName": "division_name"
  },
  {
    "id": "GREEN",
    "level": "2",
    "memSqlColumnName": "entity_color",
    "parentId": "MIDLAND",
    "parentMemSqlColumnName": "division_name"
  },
  {
    "id": "GOLD",
    "level": "2",
    "memSqlColumnName": "entity_color",
    "parentId": "MIDLAND",
    "parentMemSqlColumnName": "division_name"
  },
  {
    "id": "TRINIDAD",
    "level": "1",
    "memSqlColumnName": "division_name",
    "parentId": "EOG",
    "parentMemSqlColumnName": ""
  },
  {
    "id": "OKLAHOMA CITY",
    "level": "1",
    "memSqlColumnName": "division_name",
    "parentId": "EOG",
    "parentMemSqlColumnName": ""
  },
  {
    "id": "INTL OMAN",
    "level": "1",
    "memSqlColumnName": "division_name",
    "parentId": "EOG",
    "parentMemSqlColumnName": ""
  },
  {
    "id": "MIDSTREAM",
    "level": "1",
    "memSqlColumnName": "division_name",
    "parentId": "EOG",
    "parentMemSqlColumnName": ""
  },
  {
    "id": "SHARED SERVICES",
    "level": "1",
    "memSqlColumnName": "division_name",
    "parentId": "EOG",
    "parentMemSqlColumnName": ""
  },
  {
    "id": "ARTESIA",
    "level": "1",
    "memSqlColumnName": "division_name",
    "parentId": "EOG",
    "parentMemSqlColumnName": ""
  },
  {
    "id": "SAN ANTONIO",
    "level": "1",
    "memSqlColumnName": "division_name",
    "parentId": "EOG",
    "parentMemSqlColumnName": ""
  },
  {
    "id": "INTERNATIONAL HEADQUARTERS",
    "level": "1",
    "memSqlColumnName": "division_name",
    "parentId": "EOG",
    "parentMemSqlColumnName": ""
  },
  {
    "id": "INTL NEW VENTURES",
    "level": "1",
    "memSqlColumnName": "division_name",
    "parentId": "EOG",
    "parentMemSqlColumnName": ""
  },
  {
    "id": "FORT WORTH",
    "level": "1",
    "memSqlColumnName": "division_name",
    "parentId": "EOG",
    "parentMemSqlColumnName": ""
  }
]

I have no problem using any library that could help such as lodash, etc...



Solution 1:[1]

For every item, you need to recursively look for the parent and build the path in the process. In my solution, I created maps to make the process easier.

  • the first iteration is to normalize the data
  • the second is to build the paths for each item
  • the third is to create the new array with the paths as id and parentId

Not sure if my algorithm is the most performant, but I hope it helps you.

// utility function to create ids
const buildId = (item) =>
  item.memSqlColumnName ? `${item.memSqlColumnName}~${item.id}` : item.id;

const buildIds = (data) => {
  // normalize data for easy access
  const dataMap = data.reduce(
    (acc, item) => ({ ...acc, [buildId(item)]: item }),
    {}
  );

  // create map with paths for each item
  const pathsMap = data.reduce((acc, item) => {
    let id = buildId(item);
    let currentItem = item;
    let parentKey = null;
    while (currentItem.parentId) {
      parentKey = currentItem.parentMemSqlColumnName
        ? `${currentItem.parentMemSqlColumnName}~${currentItem.parentId}`
        : currentItem.parentId;
      id = `${parentKey}~${id}`;
      currentItem = dataMap[parentKey];
    }
    return {
      ...acc,
      [item.id]: {
        path: id,
      },
    };
  }, {});

  // create arrays with paths for item and parent
  const ids = data.map((item) => {
    return {
      id: pathsMap[item.id].path,
      parentId: item.parentId && pathsMap[item.parentId].path,
    };
  });

  return ids;
};

const data = [
  {
    id: "EOG",
    parentId: null,
  },
  {
    id: "CORPUS CHRISTI",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "CHINA",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "DENVER",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "HEADQUARTERS",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "MIDLAND",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "BLUE",
    level: "2",
    memSqlColumnName: "entity_color",
    parentId: "MIDLAND",
    parentMemSqlColumnName: "division_name",
  },
  {
    id: "AREA 2",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 1",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNKNOWN",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 7",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "RED HILLS",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "RED HILLS NORTH",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "BETA",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ZENA WEST",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "STATELINE",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "K-BAR CORRIDOR",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "NORTH DJ",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 5",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "BAKKEN LITE",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "KBAR",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "NORTH POWDER",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "CORE",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "SOUTH POWDER",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 9",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "POWDER RIVER",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "SPR OFF-CORRIDOR",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ANTELOPE",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "PEGASUS",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "SOLD",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "WEST",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ZENA",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "CORRIDOR A",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ATHENA",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "0",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 2",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ZENA EAST",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "IMPOSSIBLE",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "SDJ",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 3",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 3",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 1",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 8",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "SOUTH DJ",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "NPR CORRIDOR",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "0",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "HOBBS",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ALPHA",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "KING RANCH",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNDAUNTED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "RED HILLS SOUTH",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "HERCULES",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ROSS DRAW",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 1",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ROSS",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "DELAWARE BASIN",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "WEST",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 1",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 6",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "DELAWARE BASIN",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "CENTRAL",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "BAKKEN",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "CONAN",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 1",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 1",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "SPR CORRIDOR",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ELM COULEE",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "CARLSBAD",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "K-BAR",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 4",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "NPR OFF-CORRIDOR",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "EAST",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "BLACK",
    level: "2",
    memSqlColumnName: "entity_color",
    parentId: "MIDLAND",
    parentMemSqlColumnName: "division_name",
  },
  {
    id: "GREEN",
    level: "2",
    memSqlColumnName: "entity_color",
    parentId: "MIDLAND",
    parentMemSqlColumnName: "division_name",
  },
  {
    id: "GOLD",
    level: "2",
    memSqlColumnName: "entity_color",
    parentId: "MIDLAND",
    parentMemSqlColumnName: "division_name",
  },
  {
    id: "TRINIDAD",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "OKLAHOMA CITY",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "INTL OMAN",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "MIDSTREAM",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "SHARED SERVICES",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "ARTESIA",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "SAN ANTONIO",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "INTERNATIONAL HEADQUARTERS",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "INTL NEW VENTURES",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "FORT WORTH",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
];

console.log(buildIds(data));

Solution 2:[2]

Key points

  1. We can cache the item IDs in a JS object with original IDs mapping to structured IDs.
  2. Lucky for us we have level key and data is sorted so the parents will be processed before children.

Implementation Ideas

So we keep a temporary JS object idMap mapping the original item ID to the newly generated item ID for reference by children items later on.

While creating IDs we prepend memSqlColumnName value if available seperated by a ~. And if the item has a parent we grab and prepend the parents ID as well separated by a ~.

All this can be done efficiently in a single loop.

In case the data turns out to not be sorted by level, we can do so ourselves easily with a simple compare function on array.sort().

// Sort by level, parents before children
items.sort( ( a, b ) => +a.level - b.level )

Example implementation

/**
 * Formats data
 * @param {array} items
 * @returns {array} Restructured items
 */
function processData(items) {
  const idMap = {}; // IDs cache to get parent IDs

  /** Generates the ID */
  function generateId(item) {
    // Use memSqlColumnName if set
    let newID = item.memSqlColumnName ? `${item.memSqlColumnName}~${item.id}` : item.id;

    if (item.parentId)
      newID = idMap[item.parentId] + '~' + newID; // Prepend parent ID (if we got one).

    return idMap[item.id] = newID; // Return and cache the new ID
  }

  return items
    .map(item => ({
      id: generateId(item), // Generate ID
      parentId: idMap[item.parentId] || null, // Update parent ID from cache
    }));
}

// Data placed in an html script tags for clean code
console.log(processData(window.originalData));
<script>
// All data
window.originalData = [
  {
    id: "EOG",
    parentId: null,
  },
  {
    id: "CORPUS CHRISTI",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "CHINA",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "DENVER",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "HEADQUARTERS",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "MIDLAND",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "BLUE",
    level: "2",
    memSqlColumnName: "entity_color",
    parentId: "MIDLAND",
    parentMemSqlColumnName: "division_name",
  },
  {
    id: "AREA 2",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 1",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNKNOWN",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 7",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "RED HILLS",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "RED HILLS NORTH",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "BETA",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ZENA WEST",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "STATELINE",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "K-BAR CORRIDOR",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "NORTH DJ",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 5",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "BAKKEN LITE",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "KBAR",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "NORTH POWDER",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "CORE",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "SOUTH POWDER",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 9",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "POWDER RIVER",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "SPR OFF-CORRIDOR",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ANTELOPE",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "PEGASUS",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "SOLD",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "WEST",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ZENA",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "CORRIDOR A",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ATHENA",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "0",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 2",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ZENA EAST",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "IMPOSSIBLE",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "SDJ",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 3",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 3",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 1",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 8",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "SOUTH DJ",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "NPR CORRIDOR",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "0",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "HOBBS",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ALPHA",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "KING RANCH",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNDAUNTED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "RED HILLS SOUTH",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "HERCULES",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ROSS DRAW",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 1",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ROSS",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "DELAWARE BASIN",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "WEST",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 1",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 6",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "DELAWARE BASIN",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "CENTRAL",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "UNASSIGNED",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "BAKKEN",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "CONAN",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 1",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 1",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "SPR CORRIDOR",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "ELM COULEE",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "CARLSBAD",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "K-BAR",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "AREA 4",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "NPR OFF-CORRIDOR",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "EAST",
    level: "3",
    memSqlColumnName: "area",
    parentId: "BLUE",
    parentMemSqlColumnName: "entity_color",
  },
  {
    id: "BLACK",
    level: "2",
    memSqlColumnName: "entity_color",
    parentId: "MIDLAND",
    parentMemSqlColumnName: "division_name",
  },
  {
    id: "GREEN",
    level: "2",
    memSqlColumnName: "entity_color",
    parentId: "MIDLAND",
    parentMemSqlColumnName: "division_name",
  },
  {
    id: "GOLD",
    level: "2",
    memSqlColumnName: "entity_color",
    parentId: "MIDLAND",
    parentMemSqlColumnName: "division_name",
  },
  {
    id: "TRINIDAD",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "OKLAHOMA CITY",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "INTL OMAN",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "MIDSTREAM",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "SHARED SERVICES",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "ARTESIA",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "SAN ANTONIO",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "INTERNATIONAL HEADQUARTERS",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "INTL NEW VENTURES",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
  {
    id: "FORT WORTH",
    level: "1",
    memSqlColumnName: "division_name",
    parentId: "EOG",
    parentMemSqlColumnName: "",
  },
];
</script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 diedu
Solution 2