Find object by key valye in array of objects recursively

Viewed 53

I am trying to create a highcharts organization chart from json dynamically. For highcharts data series , format should be like below as each node will have links

enter image description here

My json file

"orgRelationData": [
        {
            "orgId": 1,
            "orgCode": "SE FIN GS",
            "orgTitle": "Finance Gas Services",
            "orgHeadPerson": 1,
            "currentLevel": 0,
            "previousLevelOrgID": null,
            "isNextLevelPresent": true,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 2,
            "orgCode": "SE FIN GS BC",
            "orgTitle": "Business Controlling",
            "orgHeadPerson": 2,
            "currentLevel": 1,
            "previousLevelOrgID": 1,
            "isNextLevelPresent": true,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 3,
            "orgCode": "SE FIN GS BC C",
            "orgTitle": "GS Central",
            "orgHeadPerson": 3,
            "currentLevel": 2,
            "previousLevelOrgID": 2,
            "isNextLevelPresent": true,
            "isClickable": true,
            "isExpandable": true
        },
        {
            "orgId": 4,
            "orgCode": "SE FIN GS BC D",
            "orgTitle": "GS Distributed",
            "orgHeadPerson": 4,
            "currentLevel": 2,
            "previousLevelOrgID": 2,
            "isNextLevelPresent": true,
            "isClickable": true,
            "isExpandable": true
        },
        {
            "orgId": 5,
            "orgCode": "SE FIN GS BC RSU",
            "orgTitle": "Regional Service Unit Business Partners",
            "orgHeadPerson": 5,
            "currentLevel": 2,
            "previousLevelOrgID": 2,
            "isNextLevelPresent": true,
            "isClickable": true,
            "isExpandable": true
        },
        {
            "orgId": 6,
            "orgCode": "SE FIN GS BC DPS",
            "orgTitle": "Data, Processes & Systems",
            "orgHeadPerson": 6,
            "currentLevel": 2,
            "previousLevelOrgID": 2,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 7,
            "orgCode": "SE FIN GS BC OC",
            "orgTitle": "Operational Controlling",
            "orgHeadPerson": 7,
            "currentLevel": 2,
            "previousLevelOrgID": 2,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 8,
            "orgCode": "SE FIN GS BC PC",
            "orgTitle": "Performance Controlling",
            "orgHeadPerson": 8,
            "currentLevel": 2,
            "previousLevelOrgID": 2,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 9,
            "orgCode": "SE FIN GS BC PROMT",
            "orgTitle": "Productivity & Cost Management",
            "orgHeadPerson": 9,
            "currentLevel": 2,
            "previousLevelOrgID": 2,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 10,
            "orgCode": "SE FIN GS BC PRM",
            "orgTitle": "Product Management Finance",
            "orgHeadPerson": 10,
            "currentLevel": 2,
            "previousLevelOrgID": 2,
            "isNextLevelPresent": true,
            "isClickable": true,
            "isExpandable": true
        },
        {
            "orgId": 11,
            "orgCode": "SE FIN GS BC VF",
            "orgTitle": "Value Flow & Foundation",
            "orgHeadPerson": 11,
            "currentLevel": 2,
            "previousLevelOrgID": 2,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 12,
            "orgCode": "SE FIN GS BC C AR",
            "orgTitle": "Analytics & External Reporting",
            "orgHeadPerson": 19,
            "currentLevel": 3,
            "previousLevelOrgID": 3,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 13,
            "orgCode": "SE FIN GS BC C CP",
            "orgTitle": "Controlling Processes",
            "orgHeadPerson": 20,
            "currentLevel": 3,
            "previousLevelOrgID": 3,
            "isNextLevelPresent": true,
            "isClickable": true,
            "isExpandable": true
        },
        {
            "orgId": 14,
            "orgCode": "SE FIN GS BC D GCO",
            "orgTitle": "Global Customer Operations",
            "orgHeadPerson": 21,
            "currentLevel": 3,
            "previousLevelOrgID": 4,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 15,
            "orgCode": "SE FIN GS BC D GSO",
            "orgTitle": "Global Service Operations",
            "orgHeadPerson": 22,
            "currentLevel": 3,
            "previousLevelOrgID": 4,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 16,
            "orgCode": "SE FIN GS BC RSU APC",
            "orgTitle": "Asia Pacific & China",
            "orgHeadPerson": 23,
            "currentLevel": 3,
            "previousLevelOrgID": 5,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 17,
            "orgCode": "SE FIN GS BC RSU EU&AF",
            "orgTitle": "Europe & Africa",
            "orgHeadPerson": 24,
            "currentLevel": 3,
            "previousLevelOrgID": 5,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 18,
            "orgCode": "SE FIN GS BC RSU LA",
            "orgTitle": "Latin America",
            "orgHeadPerson": 25,
            "currentLevel": 3,
            "previousLevelOrgID": 5,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 19,
            "orgCode": "SE FIN GS BC RSU ME",
            "orgTitle": "Middle East",
            "orgHeadPerson": 26,
            "currentLevel": 3,
            "previousLevelOrgID": 5,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 20,
            "orgCode": "SE FIN GS BC RSU NA",
            "orgTitle": "North America",
            "orgHeadPerson": 27,
            "currentLevel": 3,
            "previousLevelOrgID": 5,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 21,
            "orgCode": "SE FIN GS BC PRM FC",
            "orgTitle": "Financial Controlling",
            "orgHeadPerson": 28,
            "currentLevel": 3,
            "previousLevelOrgID": 10,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 22,
            "orgCode": "SE FIN GS BC PRM PC",
            "orgTitle": "Project Controlling",
            "orgHeadPerson": 29,
            "currentLevel": 3,
            "previousLevelOrgID": 10,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 23,
            "orgCode": "ms FC",
            "orgTitle": "m Controlling",
            "orgHeadPerson": 28,
            "currentLevel": 4,
            "previousLevelOrgID": 13,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        },
        {
            "orgId": 24,
            "orgCode": "tp PC",
            "orgTitle": "t Controlling",
            "orgHeadPerson": 29,
            "currentLevel": 4,
            "previousLevelOrgID": 13,
            "isNextLevelPresent": false,
            "isClickable": true,
            "isExpandable": false
        }
    ],

I have wrote following functions to get object by matching previousLevelOrgID and returning matched objects, then cooking array by orgId of matched object ;eg[2,4]

let dSet = orgRelationData?.map((value, idx) => {
      function objectFindByKey(array, key, value) {
        for (let i = 0; i < array.length; i++) {
          if (array[i][key] === value) {
            console.log(array[i])
            return array[i];
          }
        }
        return null;
      }
      let result_obj = objectFindByKey(
        orgRelationData,
        "orgId",
        value.previousLevelOrgID
      );
      return (value.previousLevelOrgID === eId) && [result_obj?.orgId, value?.orgId];
    }).filter(Boolean);

I have to create array of links in which previousLevelOrgID of obj matches the orgId of another obj.

How can I write above method recursively to get all objects in which previousLevelOrgID points to obj orgId?

1 Answers

You don't need a recursive function, it is enough to use a simple loop and build the data structure required by Highcharts. For example:

const orgRelationData = [...];

const data = [];
const nodes = [];

orgRelationData.forEach(dataEl => {
    const matchedEl = orgRelationData.find(
        el => el.orgId === dataEl.previousLevelOrgID
    );

    if (matchedEl) {
        data.push([dataEl.orgId, matchedEl.orgId]);
    }

    nodes.push({
        id: dataEl.orgId,
        name: dataEl.orgCode,
        title: dataEl.orgTitle
    });
});

Highcharts.chart('container', {
    ...,
    series: [{
        data,
        nodes,
        ...
    }]
});

Live demo: https://jsfiddle.net/BlackLabel/L15asmfu/

API Reference: https://api.highcharts.com/highcharts/series.organization

Related