Get an object full path to root by sub property


I am a JS beginner and now becomes clear to me that I need some way to get the subject solved, cause it’s often can do the thing faster and easier.

In my current situation I have websocket client for Nodejs (GitHub - theturtle32/WebSocket-Node: A WebSocket Implementation for Node.JS (Draft -08 through the final RFC 6455)).

let client = new WebsocketClient();
client.on('connect', (connection) => {


Or in short:

let myObj={data{tests{id:[]}}};
let result=getFullPathFunc(myOfj,'id');

So I want to see the full path by using the connection method or event.
Like : console.log(getFullPathFunc(client,‘connection’);
Expected: client.websockets…bla.bla.bla.connection(bla(typeof)).

If there is such or another way to make a things clear. It’s taking time to read and inspect about hundred of objects listed by console.log(client). Sure I can go to sources and try to find it there. But again it’s time killer action. Thanks.

I’m sorry, it’s unclear what exactly you’re trying to do.

I can see that you’ve made a websocket client, and when a connection is made, you have a callback function that receives that connection object. Please expand on what you want to do with the connection object. It sounds like you want to get access a deeply nested property and log it, is that correct?

Yes, you right, because the connection prop inside a client object, I want to get an access outside of client scope.


let client = new WebsocketClient();
client.on('connect', (connection) => {


//usually the code in such cases I saw:
client.clients.forEach(function each(connection) {
//got access
//it's no problem with ws server,  cause it has a *clients* constructor enum, but client (as I inspected in client object inside) does not contain a *clients* enum.

I’m sorry but it’s still not clear what your goal and/or problem is.

let client = new WebsocketClient();
client.on('connect', (connection) => {
  // what do you want to do here?

This code makes new client. Then it sets a “connect” event listener, so that when a “connect” event happens it will execute the callback. Right now, your callback has nothing in it.

What do you want to happen when a connection is made?

Edit: this is now redundant, but

Reiterating that it’s a bit unclear what exactly you want. But if you literally mean you want to log a string path to a key in a deeply-nested object then you use depth-first or breadth-first search to locate the key, building up a string as you traverse.

So for example:

  • given a function printPath that takes as parameters an input object, a target key, and an array of keys initialised with a root value, eg
  • fn printPath (inputObj, targetKey, path = ["root"])
  • iterate over the keys in inputObj
  • if a key is equal to targetKey, concatenate the key into the value of path. Then return the value of path joined by “.”
  • if the value of inputObj[key] is an object, then return printPath, passing in inputObj[key], targetKey and the value of path with key concatenated to it.
  • if neither of the above are true, return/throw some error, key isn’t there

This will give you a string. It’s useful for logging but that’s about it (you would have to eval it to use it, which is not something you want to do)

Assuming Dan’s on the right track, the most helpful thing you could do is give us an example input (the deeply nested object), and your expected output (what you want to see logged).

Thank you, All.

I am sorry for not providing full code:

const replicationInterval = setInterval(function ping() {
    replicationWSConfig.connect.clients.forEach(async function each(ws) {
        console.log('ws.isAlive', ws.isAlive, isReplicationWsClientConnected);
        if (ws.isAlive === false ) {
            console.log('terminated ws')
        isReplicationWsClientConnected = false;
        ws.isAlive = false;;
        await operation();
}, 8500);

This is the working code from ws server side for ping-pong. I want to do the same for client side. Logic here is if ping sent will not came back as pong to server - the connection will be dropped.

The problem with client in scope.:

let client = new WebsocketClient();
client.on('connect', (connection) => {


const pingPongHandler=(connection)=> {
//the same logic as on server.
//but the client.connection in not available to me.

I’m just looking at your client code. Please understand, I have never used this WS library and don’t know its API.

That said, why can’t you move the pingPongHandler into the “connect” callback?

let client = new WebsocketClient();
client.on('connect', (connection) => {
  //the same logic as on server.

Thanks, I was need in an expertise too, to ensure if JS or Node does not have built-in function to work with objects in such way.

As I replied to @DanCouper, I need also an expertise check, if JS or Node does not have some “magic” like ws.clients.forEach() which is amazingly handy.

There is several reasons, first for some reason if connection disappears the code inside client “connection” scope stopping work. Another one, I want to be a like a “Pro”, learning myself to effectively handling scopes. Because IMHO, the scopes are just the thing that must have.

Could somebody advise if I am on right way, my plan is to create(learn first) a class with constructor for enumerator.

So this class will accept some parameter outside, then enumerate it. So I can get an access with let myCopyClass=new Original.Class; then myCopyClass.clients.forEach()…

Thank you All for your helpful advises. In case give please heads up, if some concept available.

If you are trying to access a deeply nested property or you want test if a deeply nested property exist inside your object you can take a look at this:

This is from a library that i wrote during the time i was learning javascript with freecodecamp curriculum.Hope it helps

1 Like

I am still at a loss as what you’re trying to do, why you want a string representing a path through an object (or if you even do)

You haven’t explained what you’re actually trying to do: I realise this may be difficult if you’re writing in a second language, but you’ve got to try to explain what you’re trying to do, in simple terms.

At the minute there seems to be no relationship between the object keys thing and the websocket stuff you posted.

There is also a possiblity, reading back what you wrote a few times, that you’re misunderstanding how stuff works. Eg

I have literally no idea what you’re trying to do or why here

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.