-2

I have a JS-array of objects, which looks like this:

const arr = [
  {
    "w": 2,
    "h": 2,
    "x": 0,
    "y": 0,
    "i": "SankeyChart_3_ae2cc",
    "moved": false,
    "static": false,
    "children": []
  },
  {
    "w": 6,
    "h": 11,
    "x": 0,
    "y": 2,
    "i": "GridWrapper_6_3d4c1",  // parent elem
    "moved": false,
    "static": false,
    "children": [
      {
        "w": 4,
        "h": 6,
        "x": 0,
        "y": 0,
        "i": "tooltip_119_50038",
        "moved": false,
        "static": false,
        "children": []
      },
      {
        "w": 2,
        "h": 2,
        "x": 1,
        "y": 1,
        "i": "Highchart_135_5de23",  // child elem
        "moved": false,
        "static": false,
        "children": []
      }
    ]
  }
]

For example, I have a value of Highchart_135_5de23, which is a child elem of GridWrapper_6_3d4c1. Is it possible to find the i property of parent by knowing the i propperty of child? I was able to create a function where parent i prop is being passed directly to the function, but I now need it to be found, not hardcoded

Listopad02
  • 131
  • 1
  • 6

1 Answers1

0

Loop each entry, then find in his children the id you are searching for:

const arr = [
  {
    "w": 2,
    "h": 2,
    "x": 0,
    "y": 0,
    "i": "SankeyChart_3_ae2cc",
    "moved": false,
    "static": false,
    "children": []
  },
  {
    "w": 6,
    "h": 11,
    "x": 0,
    "y": 2,
    "i": "GridWrapper_6_3d4c1",  // parent elem
    "moved": false,
    "static": false,
    "children": [
      {
        "w": 4,
        "h": 6,
        "x": 0,
        "y": 0,
        "i": "tooltip_119_50038",
        "moved": false,
        "static": false,
        "children": []
      },
      {
        "w": 2,
        "h": 2,
        "x": 1,
        "y": 1,
        "i": "Highchart_135_5de23",  // child elem
        "moved": false,
        "static": false,
        "children": []
      }
    ]
  }
]

let thingToFind = "Highchart_135_5de23";
let parent = false;

arr.forEach(entry => {
  let search = entry.children.find(child => child.i === thingToFind)
  if (search) {
    parent = entry;
    return
  }

})

if (parent) {
  console.log(parent.i)
} else {
  console.log('No match found')
}
Wimanicesir
  • 4,606
  • 2
  • 11
  • 30
  • How is this suggestion meant to work on any level deeper than 1? – Roko C. Buljan Jul 31 '23 at 12:02
  • Does it need to? I can't find any specification of the OP that it does.. – Wimanicesir Jul 31 '23 at 12:25
  • It's right there in the code. Every Node has a `children` array. This type data structure is known as *tree*. Recursion is the most common way to iterate over such. Imagine `Highchart_135_5de23` having a child node in its `children` array. And so on, on any other depth other than 1 your script would fail. That's not the right approach. – Roko C. Buljan Jul 31 '23 at 12:47
  • That is your assumption but not confirmed anywhere. I admit this only works for 1 level, but as far as I'm concerned, it suffices. I will leave this answer as long the OP says otherwise :) – Wimanicesir Jul 31 '23 at 13:08