I want to be able to loop through all of the users, even though I don’t know what the usernames will be. So, I’d like to use a for loop something like this:
for (let i = 0; i < myJSON.users.length; i++) {
console.log(myJSON.users[i]); // should log 'somerandomusername' and on second run should log 'somerandomusername2'
console.log(myJSON.users[i].actions); // should log all actions under 'somerandomusername' and on second run should log all actions under 'somerandomusername2'
console.log(myJSON.users[i].username); // should log 'A name' and on second run should log 'Another name'
}
Is it possible to do this? I keep getting ‘undefined’ for my console logs
myJSON.users is an object, not an array. You can’t iterate through objects just using for loops like this. If you want to iterate through it, just change the brackets surrounding your user objects.
somerandomusername should contain an object that has two properties, ‘actions’ and ‘username’. Inside of ‘actions’ will be a bunch of actions that the learner took while interacting with an online course I created. Ultimately, I just want to be able to sort by username and list out all of the actions that they took. I’m new to storing data in a database so it is entirely possible that I set this up incorrectly from the beginning.
For what its worth, ‘somerandomusername’ is actually an anonymous userId assigned by Firebase when a user visits the course.
To get back to iterating through objects, it is possible with the right type of for loop. (I made a small adjustment to myJSON to make it a valid object.)
const myJSON = {
"users" : {
"somerandomusername" : {
"actions" : {
},
"username" : "A name"
},
"somerandomusername2" : {
"actions" : {
"username" : "Another name"
}
}
}
}
for (var user in myJSON.users) {
console.log('Data for user',user, 'is:', myJSON.users[user])
}
The for loop you have is the most basic, generic for loop:
for (let i = 0; i < myJSON.users.length; i++) {
console.log(i)
}
It can be used for arrays and objects. Above you have it for an array, but like @PortableStick mentioned, you don’t have an array. If you want to use this generic for loop on an object, you would have to change the condition to work with an object like this using the keys property:
for (let i = 0; i < Object.keys(myJSON.users); i++) {
console.log(i)
}
There are also for…of loops which are used for arrays
for (let index of array) {
// do something
}
There also for…in loops like @kevinSmith mentioned:
for (let property in object) {
// do something
}
So, the problem was that you were mistaking an object as an array. In the big picture, however, realize that there are many different ways to accomplish this using a wide variety of for loop types. I personally think Kevin’s solution is the most clear and straightforward.
I feel the need to mention that when you have something like a list of users, an array is the right choice. Trying to generate a list of users as properties in an object is asking for a lot of pain, and if you plan to move this to a database then there’s no chance it’s going to return you just an object. Here are some questions that come to mind when I think about this design:
How do you generate the “randomUsername” when adding a new user?
How do you avoid hash collisions?
How are you going to sort this collection, and how long will that take?
How can you efficiently search through your users to find one particular person?
How will you keep track of the number of users in your collection?
If you need your users to be in a particular order, how will you enforce this when using an object?
This may seem like making a mountain out of an anthill, but I think this is instructional. Being able to choose the right data structure is something that will make you stand out as a self-taught programmer. Perhaps you’ve got a great reason for storing users as object values - great! But imagine you’re at an interview and this project comes up. Your interviewer looks through your code, sees that you store your collection in an object and asks you, “Why did you choose to do this?”. If you’ve got a good, solid answer, then there’s no problem. If your response is to shrug and say, “I don’t know”, then you’re probably not going to get that job.
Thank you so much for taking the time to write this out. Honestly, I have never done anything like this before so I am just trying to following the docs in Firebase. I am definitely open to doing this a different/better way, I’m just not sure how.
Basically, I generate the random username when the course is launched using the anonymous authentication feature on Firebase. Throughout the course, the learner can complete a variety of interactions and each time he/she completes an action, I want the action and the time the action occurred to register in the database.
So, ideally, what I want is a structure something like:
user
action
time action occurred
So far, I haven’t been able to figure out how to use the Firebase database to do something like that. I am still learning though and I’m sure it is possible.
Thanks again for your time and direction, I appreciate it.
It also says that [a]ll Firebase Realtime Database data is stored as JSON objects. So, I was wrong to say there’s no chance that you’d get data from a database like this since, obviously, data does come from at least one database like this. Thankfully, all of the questions I had earlier are addressed by the Firebase API. It will handle hashing as you just push new data to the DB, and it can sort by any key you have in your data. So, as long as you let their servers handle the hard work, you shouldn’t have any problem with this.
your question same as me.when I start coding for several days.I think ,you might learn some data structure.such as Array,Object.and how to loop the Array and the member of Object.And then.you can easily solve these problems.
I hope this can help you.come on !!!