Learn the importance of loops from Digital Marketing Company in Noida
Introduction:
The next type of loop I would like to touch on it. Just briefly is the for loop and it's a little different than a regular for loop and I'm just going to comment so. And basically, a for loop will use the keyword for and also the keyword in and what it's used for. Let me just make a multi-line comment here for the best digital marketing company in Noida It is used to basically arbitrarily or loop through an object's properties in arbitrary order. And I say object you can use it with something like an array but really it's mostly used with objects. And the reason it's not really used with arrays is that it is. It does do things in arbitrary order.
So if you're doing something with an array chance are it's important what order it's in or the index might be important. If that's not the case if you just want to say get values that are in an object. This is what you'd use a for loop for the best digital marketing company in Noida. And we haven't really talked about objects to too much we've seen them here and there but just as you know a quick go over of what an object is it looks something like this.
Pros & Cons:
So you might have Konst users an object is wrapped in these curly brake brackets and objects are a key-value Association. So you might have an ID for example. So you know id one might be associated with the user name Sally ID too might be associated with user name Billy ID three might be actually ID for might be Timmy, for example, go for a digital marketing company in Noida. And then if we wanted to access any of these values we just have to say use the key associated with that value. So for example, if we were to cons the log out users and I want to grab out the user with the key of one. So this will give us Sallys.
If I run this we see it gives us Sally Ride if I try to access user with the idea here of I can easily do that if I try to give it a user I haven't defined. Let's see what happens. Right, it's undefined. OK so that being the case let's say we did want to loop over this object to print out all of the Keys all the values or all the keys and all the values and we would definitely use a for loop for that. So for loop looks like this for and then we can kind of initialize it using VAR or LOT and then you give it a kind of a placeholder.
Verdict:
So instead of saying something like Let It equals Digital marketing company in Noida we say let Propp, for example, end-users and then you run whatever logic you need in your block here now Propp, this is just a placeholder. And let's see what happens if I Consolo that it will see exactly what it is let's try that here for a digital marketing company in Noida. And we can see that it's giving us each key in this object. We don't need to worry about length. It already knows that. OK, I'm going to grab all the keys in this. This user objects and it's not showing us undefined or anything it's just showing us. If the keys were different right. Maybe this one had the key of A. And this one had a key of I don't know some random thing. It would show us those keys instead. And we can see that it's kind of doing it in arbitrary order. We can see that here.
Outcome:
These are numeric and these are being sorted alphabetically. I'm not sure if that's what it's doing behind the scenes that we can see it's not necessarily doing things in the same order we set them out in. So when you're dealing with objects usually order is not very important because you can just grab the key-value Association with digital marketing company in Noida We'll take a look at a way to deal with that with arrays shortly. But basically, this is what a for loop does. And we've already seen how to access out would a certain value. So if we were to do something like this console log users at you know the first loop through this is going to be accessing users at key one.
Conclusion:
Next one we users at the key to if you run this we can see that this will just give us a value for every object. So again we haven't talked about objects too much. So this is probably a bit unfamiliar to you but basically, we just need to know at this point how to access information inside of an object. And this is how we would do it. Right so you might try to do something like a For Each loop which we're going to look at in a second on an object it doesn't really work on objects works in arrays but not objects and yes are the keys as you just say they don't need to be numeric they can be anything. So this could be like a B.
Maybe we've come in and we deleted a couple of objects the on objects G. And this might be objects X for example. It doesn't really matter because we're just iterating over each of them even if the keys are not really consistent. It doesn't matter order doesn't really matter in this type of situation. And if you were to do some kind of transformative action you could do that as well. So you could say something like Concentrix new users equals users this would actually be the user for a digital marketing company in Noida. And then if you were to do user equals redacted for example redacted or deleted or something and then you cancel log out new user let's see it's giving us a warning here to be over right now to use it which is a constant. Good catch. And so we should use it. Let's hear let's see.