0

I have a nested object. I need a function. I need use it for find "parent key" Which function should i use for it ? My data :

[{
    "Name": "Main Menu",
    "Key": "1",
    "Children": [{
      "Name": "Sub Menu 1",
      "Key": "10",
      "Children": [{
        "Name": "Very Sub Menu",
        "Key": "20",
        "Children": []
      }]
    }]
  },
  {
    "Name": "Main Menu 2",
    "Key": "2",
    "Children": [{
      "Name": "Sub Menu 2",
      "Key": "11",
      "Children": [{
        "Name": "Very Sub Menu 2",
        "Key": "21",
        "Children": [{
          "Name": "Extra Small Menu",
          "Key": "30",
          "Children": []
        }]
      }]
    }]
  }
]

For example when I send my array and key (For example "10"(Sub Menu 1) for that example) I need take 1 as a result (Main Menu Key.)

Example 2 : If I give 30 as a key ; I need take 21 as a result. How can I do it ? Thanks for replies!

I tried like :

var res = myData.filter(function f(o) {
      if (o.key === dragKey) return true;

      if (o.children) {
        return (o.children = o.children.filter(f)).length;
      }
    });
    console.log(res); // Its giving main whole data main level to child level. I need just 1 upper level data.
Uğurcan Uçar
  • 127
  • 1
  • 11
  • Does this answer your question? [Javascript objects: get parent](https://stackoverflow.com/questions/2980763/javascript-objects-get-parent) – kol Aug 21 '21 at 18:48
  • @kol no brother its not answer. Its giving main level of object. I need take 1 up level not main. – Uğurcan Uçar Aug 21 '21 at 18:50
  • Have you tried at least something ? – DaniP Aug 21 '21 at 18:53
  • 1
    I’m voting to close this question because doesn't include any effort to solve the issue – DaniP Aug 21 '21 at 18:54
  • @DaniP I tried some functions but i just found main level of my object. Thats why i open that topic. – Uğurcan Uçar Aug 21 '21 at 18:55
  • Well you should include at least parts of your attempted solution then for future questions as otherwise this looks like a post "do it for me please" – DaniP Aug 21 '21 at 18:56
  • @DaniP I am trying solve that issue since 3 day. I cant found any solution thats why i open that topic brother. I search a lot, tried a lot and my last chance i asked to seniors. What is wrong with it ? – Uğurcan Uçar Aug 21 '21 at 18:56
  • @DaniP Of course its not do it for me im sorry for misunderstanding. I will add my tryings to my question. – Uğurcan Uçar Aug 21 '21 at 18:57
  • Sure don't get me wrong I get you are at this point searching for advice but for future questions take that on mind,otherwise your questions will not have the best feedback :) – DaniP Aug 21 '21 at 18:59
  • @DaniP I didnt get you wrong brother. Thanks for your warning! I will keep on my mind for sure :) – Uğurcan Uçar Aug 21 '21 at 19:00

3 Answers3

1

You could use recursion for this. Loop through the object with

const findObjectByKey = (parent, object, key) => {
    // make object optional because the first iteration is null
    if (object?.key === key) return parent
    for (const obj of Object.entries(object.children)) {
      return findObjectByKey(object, obj, key)
    }
}

const finalparent = findObjectByKey(null, object, 30)

NOTE: This is untested pseudo code just to give you an idea how I would solve it.

PRSHL
  • 1,359
  • 1
  • 11
  • 30
1

You should write a recursive function that takes an array as input. loop over array's items and check if the key matches, if it does so return a result that declares you have found the child. Then in the parent return the parent id at every call so you have the parent id at the end. Otherwise, call the function itself with the children of the current level. I hope you get the idea behind this. If you don't know how recursive functions work just google it and learn them.

1

What I would do is to use recursion to go through each nested object and check if there is some children with the key you are looking

function getParentKey(arr, key, index = 0) {
      
  if (!Array.isArray(arr[index].Children)) return;

  if (arr[index].Children.some(child => child.Key === key))
    return arr[index].Key;

  return getParentKey(arr[index], key, index + 1);
}

console.log(getParentKey(mydata, "10"));

Here a working example

const mydata = [{
  "Name": "Main Menu",
  "Key": "1",
  "Children": [{
    "Name": "Sub Menu 1",
    "Key": "10",
    "Children": [{
      "Name": "Very Sub Menu",
      "Key": "20",
      "Children": []
    }]
  }]
}, {
  "Name": "Main Menu 2",
  "Key": "2",
  "Children": [{
    "Name": "Sub Menu 2",
    "Key": "11",
    "Children": [{
      "Name": "Very Sub Menu 2",
      "Key": "21",
      "Children": [{
        "Name": "Extra Small Menu",
        "Key": "30",
        "Children": []
      }]
    }]
  }]
}]

function getParentKey(arr, key, index = 0) {
  
  if (!Array.isArray(arr[index].Children)) return;

  if (arr[index].Children.some(child => child.Key === key))
    return arr[index].Key;

  return getParentKey(arr[index], key, index + 1);
}

console.log(getParentKey(mydata, "10"));
AlexSp3
  • 2,201
  • 2
  • 7
  • 24