Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get immediate parent Id of the child id in array of nested json Object?

I need to get the parent id of the specific child.

Here is my sample JSON, If I give entity id 32 it should return 6 as parent Id and if I give 30 it should return 5 as parent id.

const arr =  [{
    "id": 0,
    "name": "My Entity",
    "children": [
        {
            "id": 1,
            "name": "MARKET",
            "children": [
                {
                    "id": 2,
                    "name": "Sales",
                    "children": [
                        {
                            "id": 3,
                            "name": "District 1",
                            "children": [
                                {
                                    "id": 5,
                                    "name": "Area 1",
                                    "children": [
                                        {
                                            "entityId": 30,
                                            "id": 26,
                                            "name": "Mumbai"
                                        },
                                        {

                                            "entityId": 31,
                                            "id": 26,
                                            "name": "Hyderabad"
                                        }
                                    ],
                                    "num": 0,
                                },
                                {
                                    "id": 6,
                                    "name": "Area 2",
                                    "children": [
                                        {
                                            "entityId": 32,
                                            "id": 32,
                                            "name": "Karnataka"
                                        },
                                        {

                                            "entityId": 33,
                                            "id": 33,
                                            "name": "Andhra Pradesh"
                                        }
                                    ],
                                    "num": 0,
                                },
                            ]
                        },
                    ]
                },
            ]
        },
    ]
}]

Here is the code I have tried

const findParent = (arr, entityId) => {
    for (let i = 0; i < arr.length; i++) {
        if (arr[i].entityId === entityId) {
            return [];
        } else if (arr[i].children && arr[i].children.length) {
            const t = findParents(arr[i].children, entityId);

            if (t !== false) {
                t.push(arr[i].id);

                return t;
            }
        }
    }

    return false;
};

findParents(arr, 30);

But it is returning like below

[
    5,
    3,
    2,
    1,
    0
]

But I want the output to be

[
  5
]

Kindly help me on this, thanks

like image 848
Ramyachinna Avatar asked Jun 17 '20 09:06

Ramyachinna


2 Answers

Replace this:

t.push(arr[i].id);

with:

if (t.length == 0) t.push(arr[i].id);
like image 130
trincot Avatar answered Nov 15 '22 01:11

trincot


I would suggest easier solution

const findParent = (arr, entityId) => {
  const children = arr.flatMap(parent => 
    (item.children || []).map(child => ({ parent, child, entityId: child.entityId }))
  )

  const res = children.find(item => item.entityId === entityId)
  return res.entityId || findChildren(res.map(v => v.child), entityId)
}
like image 30
Dmitry Reutov Avatar answered Nov 15 '22 01:11

Dmitry Reutov