0

Suppose I access a certain object that is inside an object, but I don't know what the key of the previous object is, is there any way to get the key of the parent of this object?

Basically I would like to get the relative node of a property of an object

var house = {
  bedroom: {
    bed: 'I am a bed'
  }
}

I got I'm a bed, I know her key is bed but I don't know she is in bedroom is there any way to get bedroom? just by knowing bed. It is similar to parentNode, but like in objects.

This is my wish: "bed.parentNoderesult bedroom" but in object;

Chris
  • 27
  • 4
  • 2
    you have to set a property on bedroom to reference its parent node – about14sheep Nov 20 '21 at 23:41
  • @about14sheep I don't understand, could it be more specific? Thanks – Chris Nov 20 '21 at 23:43
  • I'm curious about this part `I know her key is bed `. Can you give some more detail on how exactly do you know that the key is `bed`? – Mihai Paraschivescu Nov 20 '21 at 23:43
  • 1
    I was gonna suggest getting every second object node and Iterate to find the key by its value but that might be an issue if you have multiple bed key words which might be under a different node. – seriously Nov 20 '21 at 23:43
  • @MihaiParaschivescu I'm not OP but I think they are just referencing their example – seriously Nov 20 '21 at 23:44
  • when you create the object you will need to hold a reference to each parent node in order to look it up with bed.parentNode. – about14sheep Nov 20 '21 at 23:45
  • @MihaiParaschivescu I know I'm in bed, because I have bed, but I don't know which parent `bet` is in, I don't mean I just have `I'm a bed`, but I have the full property, key e value – Chris Nov 20 '21 at 23:47
  • @about14sheep I'm not creating this object itself, I'm getting it through ajax – Chris Nov 20 '21 at 23:49
  • @about14sheep I basically do a database lookup on a table that's in json, and my return would be the whole house object, but I need to get the parent nodes of the property itself of the house object I'm on – Chris Nov 20 '21 at 23:51
  • It's a seemingly simple question, but I'm probably not able to explain it simply. `Basically I would like to get the relative node of a property of an object` – Chris Nov 20 '21 at 23:52
  • Think about it: what would happen if you put the same value inside many different objects, what answer should you get out of your hypothetical operation then? TLDR, this is not possible unless you somehow implement the link to whatever it is you consider the parent manually. – Jon Nov 20 '21 at 23:59
  • @jon Thanks, I thought about it too, anyway thanks – Chris Nov 21 '21 at 00:01

1 Answers1

0

One way you could do this is while you are iterating through the object, store the key as a variable.

function recurseObject(obj, parentNode) {
    let result;
    if (typeOf obj !== 'object') {
        return
    }
    for(const key in obj) {
        if(//house[key] is the element you want the parent node of) {
             return parentNode
        }
        result = recurseObj(obj[key], key)
    }
    return result
}

This function simply recurses through an object passing it the previous key. When you find the value you want the parent node for you simple return it. If the logic in the if block does not find what you want it will return undefined

  • This is simply a function to iterate through the object storing the previous key. I think there would be a much cleaner approach to this, however I would need to see more of your logic
about14sheep
  • 1,813
  • 1
  • 11
  • 18
  • Ok, for today i've reached my limit, but tomorrow, analyzing your answer, i'll give you a grade, thank you – Chris Nov 21 '21 at 00:04
  • 2
    What happens in there is a bed in both the house['bedroom'] and house['cellar']? I'm asking more for the OP's sake, and not for yours, because I think what you've done is correct. – Rickard Elimää Nov 21 '21 at 00:04
  • then OP will have to get very explicit with his if statement. maybe `if(obj[key] === 'bed' && parentNode !== 'cellar')` I think the best option is try something else entirely. Where you dont need to reverse lookup an objects key. – about14sheep Nov 21 '21 at 00:08