简体   繁体   中英

Iterate over a Js array with two objects (firebase)

I'm using firebase to make a chat application. I stumbled upon a little problem. When making requests to the firebase database, firebase will return a JSON or JS object back (I'm new to this so I don't really know).

This is the structure of my database:

在此处输入图片说明

The first ID under the message tree is the client's ID which I get when a cidst connects to the application. The id's just below are the client'ID with whom the logged client chatted with and the Id below that is firebase generated ID for each message.

With this code (see below) I'm listening on the database to see if any messages have been added for notification purposes.

var ref = database.ref('messages/81k44hlET5cq2AorxLDxD1DeXV52/');
ref.on('value', gotData, errData);
function gotData(data) {

  var msgs = data.val();
  var keys = Object.keys(msgs);
  console.log(msgs);

  messages = new Array();
  timestamp = new Array();
  type = new Array();

  for(var keys in msgs) {
    if (msgs.hasOwnProperty(keys)) {
      console.log(keys + " -> " + msgs[keys]);
      messages.push(msgs[keys]);
    }
  }
}

The output of the code:

在此处输入图片说明

I'm getting an array with two objects. Until here everything works fine.

My problem is that I can't figure out how I can get my message properties from here using JavaScript since the Message-IDs are unknown.

What I mean is that I can't access the properties doing msgs[keys].id.message for example since ID is unknown.

        var ref = database.ref('messages/81k44hlET5cq2AorxLDxD1DeXV52/');
        ref.on('value', gotData, errData);
        function gotData(data)
        {


            var msgs = data.val();
            var keys = Object.keys(msgs);
            console.log(msgs);

            messages = new Array();
            timestamp = new Array();
            type = new Array();


            for(var keys in msgs)
            {
                if (msgs.hasOwnProperty(keys)) {
                    console.log(keys , " -> " , msgs[keys]);
                    messages.push(msgs[keys]);
                    var k = msgs[keys];
                    for(var keys in k){

                        console.log( k[keys]);
                    }
                    //
                }
    }

You could iterate over your JavaScript object with the .forEach method, use Object.entries for this.

In the following code snippet, I am logging all message objects

 const messages = { kjzn5ef6ke2zlfzn: { h6qjopdbgs5d6mv7f: { gd73g4d5d9dvtjzj15: { message: "k", seend: "true", timestamp: "26/6/2018 20", type: "send" }, kdaz8bamd6kprmq78: { message: "k", seend: "true", timestamp: "26/6/2018 20", type: "send" } } } }; Object.entries(messages).forEach(([id1, a]) => { Object.entries(a).forEach(([id11, b]) => { Object.entries(b).forEach(([id111, message]) => { console.log(id1, '=>', id11, '=>', id111, ':', message) }); }); }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM